ButtonStyle.h
1 /* Copyright (C) 2016 Laurentiu-Gheorghe Crisan
2  Copyright (C) 2016 Marc Boris Duerner
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Lesser General Public
6  License as published by the Free Software Foundation; either
7  version 2.1 of the License, or (at your option) any later version.
8 
9  As a special exception, you may use this file as part of a free
10  software library without restriction. Specifically, if other files
11  instantiate templates or use macros or inline functions from this
12  file, or you compile this file and link it with other files to
13  produce an executable, this file does not by itself cause the
14  resulting executable to be covered by the GNU General Public
15  License. This exception does not however invalidate any other
16  reasons why the executable file might be covered by the GNU Library
17  General Public License.
18 
19  This library is distributed in the hope that it will be useful,
20  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  Lesser General Public License for more details.
23 
24  You should have received a copy of the GNU Lesser General Public
25  License along with this library; if not, write to the Free Software
26  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
27  MA 02110-1301 USA
28 */
29 
30 #ifndef Pt_Forms_ButtonStyle_h
31 #define Pt_Forms_ButtonStyle_h
32 
33 #include <Pt/Forms/Style.h>
34 
35 namespace Pt {
36 
37 namespace Forms {
38 
44 class PT_FORMS_API ButtonStyleOptions
45 {
46  public:
50 
53  bool hasOverrides() const;
54 
61  std::size_t generation() const;
62 
65  const Gfx::Brush* foreground() const;
66 
69  void setForeground(const Gfx::Brush& brush);
70 
73  const Gfx::Pen* contour() const;
74 
77  void setContour(const Gfx::Pen& pen);
78 
82  const Gfx::Color* accentColor() const;
83 
86  void setAccentColor(const Gfx::Color& color);
87 
91  const Gfx::Color* highlightColor() const;
92 
95  void setHighlightColor(const Gfx::Color& color);
96 
99  const Gfx::Color* textColor() const;
100 
103  void setTextColor(const Gfx::Color& color);
104 
107  const Gfx::Font* font() const;
108 
111  void setFont(const Gfx::Font& font);
112 
115  void setFontSize(std::size_t size);
116 
120 
124 
127  Gfx::Font getFont(const Gfx::Font& base) const;
128 
129  private:
130  enum StyleOverride
131  {
132  Foreground = 0x01,
133  Contour = 0x02,
134  AccentColor = 0x04,
135  HighlightColor = 0x08,
136  TextColor = 0x10,
137  Font = 0x20
138  };
139 
140  bool hasOverride(StyleOverride mask) const;
141 
142  void setOverride(StyleOverride mask);
143 
144  private:
145  AutoPtr<Gfx::Brush> _foreground;
146  AutoPtr<Gfx::Pen> _contour;
147  AutoPtr<Gfx::Color> _accentColor;
148  AutoPtr<Gfx::Color> _highlightColor;
149  AutoPtr<Gfx::Color> _textColor;
150  FontOption _font;
151  std::size_t _generation;
152  unsigned _overrides;
153 };
154 
160 class PT_FORMS_API ButtonState
161 {
162  public:
166 
169  bool isEnabled() const;
170 
173  void setEnabled(bool value);
174 
177  bool isHovered() const;
178 
181  void setHovered(bool value);
182 
185  bool isFocused() const;
186 
189  void setFocused(bool value);
190 
193  bool isPressed() const;
194 
197  void setPressed(bool value);
198 
201  bool isFlat() const;
202 
205  void setFlat(bool value);
206 
207  private:
208  bool _enabled;
209  bool _hovered;
210  bool _focused;
211  bool _pressed;
212  bool _flat;
213 };
214 
220 class PT_FORMS_API ButtonRenderer : public Style::Facet
221 {
222  public:
223  explicit ButtonRenderer(std::size_t refs = 0);
224 
225  virtual ~ButtonRenderer();
226 
230 
237  void prepare(const StyleOptions& options,
238  const ButtonStyleOptions& buttonOptions);
239 
240  public:
247  Direction direction,
248  const Gfx::SizeF& iconSize,
249  const Gfx::SizeF& textSize);
250 
254  const Gfx::SizeF& contentSize);
255 
259  const Gfx::RectF& frameRect);
260 
264  const String& text,
265  const Gfx::PointF& textPos,
266  const Gfx::FontMetrics& fontMetrics,
267  String::size_type mnemonicIndex);
268 
271  const Painter& textPainter(PaintSurface& surface);
272 
279  const Gfx::RectF& contentRect,
280  Direction direction,
281  const Gfx::SizeF& iconSize,
282  const Gfx::SizeF& textSize,
283  Gfx::RectF& iconRect,
284  Gfx::RectF& textRect);
285 
288  void prepareIcon(const Gfx::Image& icon,
289  Pixmap& picture,
290  const ButtonState& state) const;
291 
298  const Gfx::RectF& rect,
299  const ButtonState& state);
300 
303  void renderChrome(PaintContext& context,
304  const Gfx::RectF& rect,
305  const ButtonState& state);
306 
309  void renderText(PaintContext& context,
310  const Gfx::RectF& rect,
311  const String& text,
312  const Gfx::PointF& pos,
313  const ButtonState& state);
314 
318  const Gfx::RectF& rect,
319  const Gfx::RectF& mnemonic,
320  const ButtonState& state);
321 
324  void renderIcon(PaintContext& context,
325  const Gfx::RectF& rect,
326  const Pixmap& picture,
327  const Gfx::PointF& pos,
328  const ButtonState& state);
329 
330  protected:
333  virtual void onReset(const StyleOptions& options);
334 
335  virtual ButtonRenderer* onCreate() const = 0;
336 
337  virtual void onPrepare(const StyleOptions& options,
338  const ButtonStyleOptions& buttonOptions) = 0;
339 
340  virtual Gfx::SizeF onMeasureFrame(PaintSurface& surface,
341  const Gfx::SizeF& contentSize) = 0;
342 
343  virtual Gfx::SizeF onMeasureContent(PaintSurface& surface,
344  Direction direction,
345  const Gfx::SizeF& iconSize,
346  const Gfx::SizeF& textSize) = 0;
347 
348  virtual Gfx::RectF onLayoutFrame(PaintSurface& surface,
349  const Gfx::RectF& frameRect) = 0;
350 
351  virtual Gfx::RectF onLayoutMnemonic(PaintSurface& surface,
352  const String& text,
353  const Gfx::PointF& textPos,
354  const Gfx::FontMetrics& fontMetrics,
355  String::size_type mnemonicIndex) = 0;
356 
357  virtual const Painter& onGetTextPainter(PaintSurface& surface) = 0;
358 
359  virtual void onLayoutContent(PaintSurface& surface,
360  const Gfx::RectF& contentRect,
361  Direction direction,
362  const Gfx::SizeF& iconSize,
363  const Gfx::SizeF& textSize,
364  Gfx::RectF& iconRect,
365  Gfx::RectF& textRect) = 0;
366 
367  virtual void onRenderBackground(PaintContext& context,
368  const Gfx::RectF& rect,
369  const ButtonState& state) = 0;
370 
371  virtual void onPrepareIcon(const Gfx::Image& icon,
372  Pixmap& picture,
373  const ButtonState& state) const = 0;
374 
375  virtual void onRenderChrome(PaintContext& context,
376  const Gfx::RectF& rect,
377  const ButtonState& state) = 0;
378 
379  virtual void onRenderText(PaintContext& context,
380  const Gfx::RectF& rect,
381  const String& text,
382  const Gfx::PointF& pos,
383  const ButtonState& state) = 0;
384 
385  virtual void onRenderMnemonic(PaintContext& context,
386  const Gfx::RectF& rect,
387  const Gfx::RectF& mnemonic,
388  const ButtonState& state) = 0;
389 
390  virtual void onRenderIcon(PaintContext& context,
391  const Gfx::RectF& rect,
392  const Pixmap& picture,
393  const Gfx::PointF& pos,
394  const ButtonState& state) = 0;
395 };
396 
403 class PT_FORMS_API ButtonStyle : public StyleBinder<ButtonRenderer,
404  ButtonStyleOptions>
405 {
406  public:
410 };
411 
412 } // namespace
413 
414 } // namespace
415 
416 #endif
Core module.
Definition: Allocator.h:33
Stores the widget-local visual state for a push button.
Definition: ButtonStyle.h:161
void setFlat(bool value)
Sets whether the button uses flat rendering.
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 prepare(const StyleOptions &options, const ButtonStyleOptions &buttonOptions)
Applies the widget-local button style overrides to this renderer.
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.
Common renderer-binding controller for Forms style slices.
Definition: Style.h:272
void prepareIcon(const Gfx::Image &icon, Pixmap &picture, const ButtonState &state) const
Converts icon to a state-appropriate pixmap written to picture.
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.
const Gfx::Brush * foreground() const
Returns the local foreground override or 0 if none is set.
void setForeground(const Gfx::Brush &brush)
Sets the local foreground override.
Metrics that describe a font face at a given size.
Definition: FontMetrics.h:46
const Gfx::Color * accentColor() const
Returns the local accent color override or 0 if none is set.
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
Stores the widget-local style overrides for a push button.
Definition: ButtonStyle.h:45
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
void setFontSize(std::size_t size)
Sets the local font size override.
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.
const Gfx::Color * textColor() const
Returns the local text color override or 0 if none is set.
Size with floating-point width and height.
Definition: Size.h:47
Policy based Auto pointer.
Definition: SmartPtr.h:291
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
ButtonStyleOptions()
Constructs empty local button style options.
void setPressed(bool value)
Sets whether the button is pressed.
void setFont(const Gfx::Font &font)
Sets the complete local font override.
std::size_t generation() const
Returns the current local override generation.
const Gfx::Color * highlightColor() const
Returns the local highlight color override or 0 if none is set.
Composable font override slice for widget-local style options.
Definition: StyleOptions.h:344
Weight
Describes the requested font weight.
Definition: Font.h:61
const Gfx::Font * font() const
Returns the local font override data or 0 if none is set.
void setEnabled(bool value)
Sets whether the button is enabled.
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: ButtonStyle.h:221
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 setFontSlant(Gfx::Font::Slant slant)
Sets the local font slant override.
Gfx::Font getFont(const Gfx::Font &base) const
Resolves the effective font against the given style default.
void setContour(const Gfx::Pen &pen)
Sets the local contour override.
Binds a push button to the currently active renderer.
Definition: ButtonStyle.h:405
ButtonStyle()
Constructs an unbound button style controller.
Font request used for text drawing and measurement.
Definition: Font.h:56
Basic image.
Definition: Image.h:52
Stores the global theme option values shared across styles.
Definition: StyleOptions.h:186
bool isFlat() const
Returns true if the button uses flat rendering.
Standard color type.
Definition: Color.h:47
const Gfx::Pen * contour() const
Returns the local contour override or 0 if none is set.
void setHighlightColor(const Gfx::Color &color)
Sets the local highlight color override.
void setTextColor(const Gfx::Color &color)
Sets the local text color override.
Unicode capable basic_string.
Definition: Api-String.h:44
void setAccentColor(const Gfx::Color &color)
Sets the local accent color override.
Fill description for shapes and text.
Definition: Brush.h:145
ButtonState()
Constructs an empty button state.
bool isFocused() const
Returns true if the button currently has focus.
Rect with floating-point coordinates.
Definition: Rect.h:47
Back buffer drawing surface.
Definition: Pixmap.h:77
bool hasOverrides() const
Returns true if any local style override is present.
void setFocused(bool value)
Sets whether the button has focus.
void setFontWeight(Gfx::Font::Weight weight)
Sets the local font weight override.
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.
virtual void onReset(const StyleOptions &options)
Resets the shared button renderer to global defaults.
void renderChrome(PaintContext &context, const Gfx::RectF &rect, const ButtonState &state)
Paints the button background and border within the button bounding rect.
void renderBackground(PaintContext &context, const Gfx::RectF &rect, const ButtonState &state)
Paints the background fill within the widget rect.