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
36namespace Pt {
37
38namespace Forms {
39
48class PT_FORMS_API ButtonState
49{
50 public:
54
57 bool isEnabled() const;
58
61 void setEnabled(bool value);
62
65 bool isHovered() const;
66
69 void setHovered(bool value);
70
73 bool isFocused() const;
74
77 void setFocused(bool value);
78
81 bool isPressed() const;
82
85 void setPressed(bool value);
86
89 bool isFlat() const;
90
93 void setFlat(bool value);
94
95 private:
96 bool _enabled;
97 bool _hovered;
98 bool _focused;
99 bool _pressed;
100 bool _flat;
101};
102
123class PT_FORMS_API ButtonRenderer : public Renderer
124{
125 public:
128 explicit ButtonRenderer(std::size_t refs = 0);
129
133
137
138 public:
144 Gfx::SizeF measureContent(PaintSurface& surface,
145 Direction direction,
146 const Gfx::SizeF& iconSize,
147 const Gfx::SizeF& textSize);
148
151 Gfx::SizeF measureFrame(PaintSurface& surface,
152 const Gfx::SizeF& contentSize);
153
156 Gfx::RectF layoutFrame(PaintSurface& surface,
157 const Gfx::RectF& frameRect);
158
161 Gfx::RectF layoutMnemonic(PaintSurface& surface,
162 const String& text,
163 const Gfx::PointF& textPos,
164 const Gfx::FontMetrics& fontMetrics,
165 String::size_type mnemonicIndex);
166
170
177 const Gfx::RectF& contentRect,
178 Direction direction,
179 const Gfx::SizeF& iconSize,
180 const Gfx::SizeF& textSize,
181 Gfx::RectF& iconRect,
182 Gfx::RectF& textRect);
183
186 void prepareIcon(const Gfx::Image& icon,
187 Pixmap& picture,
188 const ButtonState& state) const;
189
196 const Gfx::RectF& rect,
197 const ButtonState& state);
198
202 const Gfx::RectF& rect,
203 const ButtonState& state);
204
208 const Gfx::RectF& rect,
209 const String& text,
210 const Gfx::PointF& pos,
211 const ButtonState& state);
212
216 const Gfx::RectF& rect,
217 const Gfx::RectF& mnemonic,
218 const ButtonState& state);
219
223 const Gfx::RectF& rect,
224 const Pixmap& picture,
225 const Gfx::PointF& pos,
226 const ButtonState& state);
227
228 protected:
231 virtual ButtonRenderer* onCreate() const = 0;
232
235 virtual void onReset(const StyleOptions& options) = 0;
236
239 virtual Gfx::SizeF onMeasureFrame(PaintSurface& surface,
240 const Gfx::SizeF& contentSize) = 0;
241
244 virtual Gfx::SizeF onMeasureContent(PaintSurface& surface,
245 Direction direction,
246 const Gfx::SizeF& iconSize,
247 const Gfx::SizeF& textSize) = 0;
248
251 virtual Gfx::RectF onLayoutFrame(PaintSurface& surface,
252 const Gfx::RectF& frameRect) = 0;
253
256 virtual Gfx::RectF onLayoutMnemonic(PaintSurface& surface,
257 const String& text,
258 const Gfx::PointF& textPos,
259 const Gfx::FontMetrics& fontMetrics,
260 String::size_type mnemonicIndex) = 0;
261
264 virtual const Painter& onGetTextPainter(PaintSurface& surface) = 0;
265
268 virtual void onLayoutContent(PaintSurface& surface,
269 const Gfx::RectF& contentRect,
270 Direction direction,
271 const Gfx::SizeF& iconSize,
272 const Gfx::SizeF& textSize,
273 Gfx::RectF& iconRect,
274 Gfx::RectF& textRect) = 0;
275
278 virtual void onRenderBackground(PaintContext& context,
279 const Gfx::RectF& rect,
280 const ButtonState& state) = 0;
281
284 virtual void onPrepareIcon(const Gfx::Image& icon,
285 Pixmap& picture,
286 const ButtonState& state) const = 0;
287
290 virtual void onRenderChrome(PaintContext& context,
291 const Gfx::RectF& rect,
292 const ButtonState& state) = 0;
293
296 virtual void onRenderText(PaintContext& context,
297 const Gfx::RectF& rect,
298 const String& text,
299 const Gfx::PointF& pos,
300 const ButtonState& state) = 0;
301
304 virtual void onRenderMnemonic(PaintContext& context,
305 const Gfx::RectF& rect,
306 const Gfx::RectF& mnemonic,
307 const ButtonState& state) = 0;
308
311 virtual void onRenderIcon(PaintContext& context,
312 const Gfx::RectF& rect,
313 const Pixmap& picture,
314 const Gfx::PointF& pos,
315 const ButtonState& state) = 0;
316};
317
333class PT_FORMS_API ButtonStyler : public Styler
334{
335 public:
339
342 const Gfx::Brush& foreground() const;
343
346 void setForeground(const Gfx::Brush& brush);
347
350 const Gfx::Pen& contour() const;
351
354 void setContour(const Gfx::Pen& pen);
355
358 const Gfx::Color& accentColor() const;
359
362 void setAccentColor(const Gfx::Color& color);
363
367
370 void setHighlightColor(const Gfx::Color& color);
371
374 const Gfx::Color& textColor() const;
375
378 void setTextColor(const Gfx::Color& color);
379
383
386 void setFont(const Gfx::Font& font);
387
390 void setFontSize(std::size_t size);
391
395
399
404 bool prepareIcon(const Gfx::Image& icon,
405 Pixmap& picture,
406 const ButtonState& state) const;
407
411 const String& text,
412 Gfx::TextMetrics& textMetrics,
413 Gfx::FontMetrics& fontMetrics) const;
414
417 Gfx::SizeF measureContent(PaintSurface& surface,
418 Direction direction,
419 const Gfx::SizeF& iconSize,
420 const Gfx::SizeF& textSize) const;
421
424 Gfx::SizeF measureFrame(PaintSurface& surface,
425 const Gfx::SizeF& contentSize) const;
426
429 Gfx::RectF layoutFrame(PaintSurface& surface,
430 const Gfx::RectF& frameRect) const;
431
435 const Gfx::RectF& contentRect,
436 Direction direction,
437 const Gfx::SizeF& iconSize,
438 const Gfx::SizeF& textSize,
439 Gfx::RectF& iconRect,
440 Gfx::RectF& textRect) const;
441
444 Gfx::RectF layoutMnemonic(PaintSurface& surface,
445 const String& text,
446 const Gfx::PointF& textPos,
447 const Gfx::FontMetrics& fontMetrics,
448 String::size_type mnemonicIndex) const;
449
453 const Gfx::RectF& rect,
454 const ButtonState& state) const;
455
459 const Gfx::RectF& rect,
460 const ButtonState& state) const;
461
465 const Gfx::RectF& rect,
466 const String& text,
467 const Gfx::PointF& pos,
468 const ButtonState& state) const;
469
473 const Gfx::RectF& rect,
474 const Gfx::RectF& mnemonic,
475 const ButtonState& state) const;
476
480 const Gfx::RectF& rect,
481 const Pixmap& picture,
482 const Gfx::PointF& pos,
483 const ButtonState& state) const;
484
487 void setRenderer(ButtonRenderer* renderer = 0);
488
494
497 const StyleOptions& options() const;
498
499 protected:
502 virtual StyleOptions& onBindOptions(const StyleOptions& styleOptions);
503
506 virtual Renderer* onStyleRenderer(const Style& style);
507
510 virtual Renderer* onCreateRenderer(const Style& style);
511
512 private:
513 FacetPtr<ButtonRenderer> _renderer;
514 StyleOptions _options;
515};
516
517} // namespace
518
519} // namespace
520
521#endif
Renders the look of a push button.
Definition ButtonStyler.h:124
void renderChrome(PaintContext &context, const Gfx::RectF &rect, const ButtonState &state)
Paints the button background and border within the button bounding rect.
virtual ~ButtonRenderer()
Destroys the renderer.
virtual void onRenderChrome(PaintContext &context, const Gfx::RectF &rect, const ButtonState &state)=0
Paints the button chrome for state.
virtual void onRenderMnemonic(PaintContext &context, const Gfx::RectF &rect, const Gfx::RectF &mnemonic, const ButtonState &state)=0
Paints the mnemonic underline for state.
virtual const Painter & onGetTextPainter(PaintSurface &surface)=0
Returns a painter with the current font and text color.
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.
Gfx::SizeF measureFrame(PaintSurface &surface, const Gfx::SizeF &contentSize)
Returns the outer size including button decoration for contentSize on surface.
ButtonRenderer * create() const
Creates a new default-constructed instance that the caller owns.
virtual Gfx::RectF onLayoutMnemonic(PaintSurface &surface, const String &text, const Gfx::PointF &textPos, const Gfx::FontMetrics &fontMetrics, String::size_type mnemonicIndex)=0
Returns the mnemonic underline rectangle.
void prepareIcon(const Gfx::Image &icon, Pixmap &picture, const ButtonState &state) const
Converts icon to a state-appropriate pixmap written to picture.
Gfx::RectF layoutFrame(PaintSurface &surface, const Gfx::RectF &frameRect)
Returns the content rectangle within the outer frameRect.
virtual void onRenderIcon(PaintContext &context, const Gfx::RectF &rect, const Pixmap &picture, const Gfx::PointF &pos, const ButtonState &state)=0
Paints picture at pos for state.
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.
ButtonRenderer(std::size_t refs=0)
Constructs a renderer with reference count refs.
virtual Gfx::SizeF onMeasureFrame(PaintSurface &surface, const Gfx::SizeF &contentSize)=0
Measures the frame enclosing contentSize.
virtual void onRenderBackground(PaintContext &context, const Gfx::RectF &rect, const ButtonState &state)=0
Paints the background fill for state.
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 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.
virtual Gfx::RectF onLayoutFrame(PaintSurface &surface, const Gfx::RectF &frameRect)=0
Returns the content rectangle within frameRect.
virtual Gfx::SizeF onMeasureContent(PaintSurface &surface, Direction direction, const Gfx::SizeF &iconSize, const Gfx::SizeF &textSize)=0
Measures icon and text arranged by direction.
const Painter & textPainter(PaintSurface &surface)
Returns a painter with the current font and text color applied for surface.
virtual void onRenderText(PaintContext &context, const Gfx::RectF &rect, const String &text, const Gfx::PointF &pos, const ButtonState &state)=0
Paints text at pos for state.
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.
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.
virtual ButtonRenderer * onCreate() const =0
Creates a new instance of the same concrete type.
virtual void onReset(const StyleOptions &options)=0
Resets this facet with options.
virtual void onPrepareIcon(const Gfx::Image &icon, Pixmap &picture, const ButtonState &state) const =0
Prepares picture from icon for state.
virtual void onLayoutContent(PaintSurface &surface, const Gfx::RectF &contentRect, Direction direction, const Gfx::SizeF &iconSize, const Gfx::SizeF &textSize, Gfx::RectF &iconRect, Gfx::RectF &textRect)=0
Partitions contentRect into icon and text rectangles.
void renderBackground(PaintContext &context, const Gfx::RectF &rect, const ButtonState &state)
Paints the background fill within the widget rect.
Transient visual state of a push button.
Definition ButtonStyler.h:49
void setPressed(bool value)
Sets whether the button is pressed.
bool isFocused() const
Returns true if the button currently has focus.
void setFocused(bool value)
Sets whether the button has focus.
bool isPressed() const
Returns true if the button is currently pressed.
void setHovered(bool value)
Sets whether the pointer is currently hovering the button.
bool isFlat() const
Returns true if the button uses flat rendering.
bool isEnabled() const
Returns true if the button is currently enabled.
void setEnabled(bool value)
Sets whether the button is enabled.
void setFlat(bool value)
Sets whether the button uses flat rendering.
ButtonState()
Constructs an empty button state.
bool isHovered() const
Returns true if the pointer is currently hovering the button.
Gfx::SizeF measureContent(PaintSurface &surface, Direction direction, const Gfx::SizeF &iconSize, const Gfx::SizeF &textSize) const
Measures icon and text content arranged by direction.
StyleOptions & options()
Returns the bound effective button options.
const Gfx::Color & highlightColor() const
Returns the effective highlight color.
const Gfx::Color & textColor() const
Returns the effective text color.
virtual Renderer * onStyleRenderer(const Style &style)
Returns the shared button renderer from style, or 0.
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.
void setHighlightColor(const Gfx::Color &color)
Sets the widget-local highlight color to color.
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 setForeground(const Gfx::Brush &brush)
Sets the widget-local foreground brush to brush.
ButtonStyler()
Constructs an unbound button styler.
const Gfx::Brush & foreground() const
Returns the effective foreground brush.
Gfx::Font font() const
Returns the effective font.
void renderBackground(PaintContext &context, const Gfx::RectF &rect, const ButtonState &state) const
Renders the button background within rect for state.
virtual Renderer * onCreateRenderer(const Style &style)
Creates an independent clone of the style renderer, or 0.
void renderChrome(PaintContext &context, const Gfx::RectF &rect, const ButtonState &state) const
Renders the button chrome within rect for state.
bool prepareIcon(const Gfx::Image &icon, Pixmap &picture, const ButtonState &state) const
Prepares picture from icon for state.
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.
void setRenderer(ButtonRenderer *renderer=0)
Assigns a specific button renderer.
Gfx::RectF layoutFrame(PaintSurface &surface, const Gfx::RectF &frameRect) const
Returns the frame content rectangle within frameRect.
const Gfx::Color & accentColor() const
Returns the effective accent color.
Gfx::SizeF measureFrame(PaintSurface &surface, const Gfx::SizeF &contentSize) const
Measures the frame enclosing contentSize.
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.
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.
const StyleOptions & options() const
Returns the bound effective button options.
void setTextColor(const Gfx::Color &color)
Sets the widget-local text color to color.
void setFont(const Gfx::Font &font)
Sets the widget-local font to font.
const Gfx::Pen & contour() const
Returns the effective contour pen.
void setContour(const Gfx::Pen &pen)
Sets the widget-local contour pen to pen.
void setFontWeight(Gfx::Font::Weight weight)
Sets the widget-local font weight to weight.
virtual StyleOptions & onBindOptions(const StyleOptions &styleOptions)
Binds the overlay to styleOptions and returns it.
void setAccentColor(const Gfx::Color &color)
Sets the widget-local accent color to color.
void measureText(PaintSurface &surface, const String &text, Gfx::TextMetrics &textMetrics, Gfx::FontMetrics &fontMetrics) const
Measures text and stores its metrics for surface.
void setFontSize(std::size_t size)
Sets the widget-local font size to size.
void setFontSlant(Gfx::Font::Slant slant)
Sets the widget-local font slant to slant.
Names a flow direction.
Definition Direction.h:52
Manages a reference-counted Style::Facet.
Definition Style.h:79
Active painting session on a Forms paint surface.
Definition PaintContext.h:51
Forms render target for painting.
Definition PaintSurface.h:50
Draws on a Forms paint surface or paint context.
Definition Painter.h:53
Off-screen Forms paint surface.
Definition Pixmap.h:130
Provides cloneable drawing for one control family.
Definition Renderer.h:61
Renderer(const std::type_info &ti, std::size_t refs=0)
Constructor.
Definition Renderer.h:65
Stores named appearance options.
Definition StyleOptions.h:646
Stores renderer facets for the active theme.
Definition Style.h:203
Styler()
Constructor.
Fill color, gradient or texture.
Definition Brush.h:151
8-bit ARGB color.
Definition Color.h:65
Ascent, descent and line height of a font.
Definition FontMetrics.h:50
Font family, size and style.
Definition Font.h:63
Slant
Describes the requested font slant.
Definition Font.h:83
Weight
Describes the requested font weight.
Definition Font.h:68
Outline color, width and style.
Definition Pen.h:59
Width and bounds of a text run.
Definition TextMetrics.h:49
Unicode capable basic_string.
Definition Api-String.h:67
Graphical user interfaces.
Definition ActivateEvent.h:40
Core module.
Definition Allocator.h:33