PushButton.h
1 /* Copyright (C) 2016 Marc Boris Duerner
2  Copyright (C) 2016 Laurentiu-Gheorghe Crisan
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_PushButton_H
31 #define Pt_Forms_PushButton_H
32 
33 #include <Pt/Forms/Button.h>
34 #include <Pt/Forms/ButtonStyler.h>
35 #include <Pt/Forms/Icon.h>
36 #include <Pt/Forms/PixmapSurface.h>
37 #include <Pt/Forms/Direction.h>
38 #include <Pt/Gfx/Image.h>
39 #include <Pt/Gfx/Brush.h>
40 #include <Pt/Gfx/Pen.h>
41 #include <Pt/SmartPtr.h>
42 
43 namespace Pt {
44 
45 namespace Forms {
46 
76 class PT_FORMS_API PushButton : public Button
77 {
78  public:
79  typedef Button Base;
80 
81  public:
85 
88  virtual ~PushButton();
89 
92  bool isPressed() const;
93 
96  void setPressed(bool pressed);
97 
100  bool isToggle() const;
101 
104  void setToggle(bool toggle);
105 
110  void setIcon(const Icon& icon, const Gfx::SizeF& iconSize);
111 
114  bool isFlat() const;
115 
118  void setFlat(bool f);
119 
123 
124  public:
127  const Gfx::Brush& foreground() const;
128 
131  void setForeground(const Gfx::Brush& b);
132 
135  const Gfx::Pen& contour() const;
136 
139  void setContour(const Gfx::Pen& p);
140 
143  const Gfx::Color& accentColor() const;
144 
147  void setAccentColor(const Gfx::Color& color);
148 
151  const Gfx::Color& highlightColor() const;
152 
156 
159  const Gfx::Color& textColor() const;
160 
163  void setTextColor(const Gfx::Color& color);
164 
167  Gfx::Font font() const;
168 
171  void setFont(const Gfx::Font& font);
172 
175  void setFontSize(std::size_t size);
176 
180 
184 
190  void setRenderer(ButtonRenderer* renderer);
191 
192  protected:
195  virtual void onPressed();
196 
199  virtual void onReleased();
200 
203  virtual void onCanceled();
204 
205  protected:
208  virtual void onRescaleEvent(const RescaleEvent& ev);
209 
210  protected:
217  virtual void onInvalidate();
218 
221  virtual Gfx::SizeF onMeasure(const SizePolicy& policy);
222 
225  virtual void onLayout(const Gfx::RectF& rect);
226 
229  virtual void onPaint(PaintContext& context, const Gfx::RectF& updateRect);
230 
235  virtual void onPaintBackground(PaintContext& context,
236  const Gfx::RectF& rect,
237  const ButtonState& state);
238 
243  virtual void onPaintFrame(PaintContext& context,
244  const Gfx::RectF& rect,
245  const ButtonState& state);
246 
251  virtual void onPaintIcon(PaintContext& context,
252  const Gfx::RectF& rect,
253  const Pixmap& picture,
254  const Gfx::PointF& pos,
255  const ButtonState& state);
256 
261  virtual void onPaintText(PaintContext& context,
262  const Gfx::RectF& rect,
263  const String& text,
264  const Gfx::PointF& pos,
265  const ButtonState& state);
266 
271  virtual void onPaintMnemonic(PaintContext& context,
272  const Gfx::RectF& rect,
273  const Gfx::RectF& mnemonic,
274  const ButtonState& state);
275 
276  private:
277  const ButtonState& buttonState();
278 
279  private:
280  bool _isToggle;
281  bool _isBeingToggled;
282  Direction _direction;
283  Icon _icon;
284  bool _iconInvalid;
285  PixmapSurface _picture;
286 
287  ButtonState _buttonState;
288  ButtonStyler _styler;
289 
290  Gfx::SizeF _iconSize;
291  Gfx::PointF _textPos;
292  Gfx::PointF _iconPos;
293  Gfx::RectF _contentRect;
294  Gfx::RectF _iconRect;
295  Gfx::RectF _textRect;
296  Gfx::RectF _mnemonicRect;
297  Gfx::TextMetrics _textMetrics;
298  Gfx::FontMetrics _fontMetrics;
299  Gfx::SizeF _measuredIconSize;
300 };
301 
302 } // namespace
303 
304 } // namespace
305 
306 #endif
void setForeground(const Gfx::Brush &b)
Sets the widget-local foreground brush to b.
Core module.
Definition: Allocator.h:33
const Gfx::Color & textColor() const
Returns the effective text color.
virtual void onPaintMnemonic(PaintContext &context, const Gfx::RectF &rect, const Gfx::RectF &mnemonic, const ButtonState &state)
Paints the button mnemonic layer.
virtual void onPaintText(PaintContext &context, const Gfx::RectF &rect, const String &text, const Gfx::PointF &pos, const ButtonState &state)
Paints the button text layer.
void setToggle(bool toggle)
Sets whether the button stays pressed after a click.
Represents an image at one or more logical sizes.
Definition: Icon.h:144
Transient visual state of a push button.
Definition: ButtonStyler.h:49
void setTextColor(const Gfx::Color &color)
Sets the widget-local text color to color.
Attributes for the drawing of outlines.
Definition: Pen.h:54
bool isPressed() const
Returns true if the button is visually pressed.
Gfx::Font font() const
Returns the effective font.
virtual Gfx::SizeF onMeasure(const SizePolicy &policy)
Measures icon, caption, and frame.
virtual void onInvalidate()
Binds the styler and prepares the icon.
virtual ~PushButton()
Destroys the button.
bool isFlat() const
Returns true if the button uses a flat face.
void setLayout(Direction d)
Sets the direction of icon and caption to d.
void setHighlightColor(const Gfx::Color &c)
Sets the widget-local highlight color to c.
void setFlat(bool f)
Sets whether the button uses a flat face.
Metrics that describe a font face at a given size.
Definition: FontMetrics.h:46
void setFontWeight(Gfx::Font::Weight weight)
Sets the widget-local font weight to weight.
Slant
Describes the requested font slant.
Definition: Font.h:76
void setContour(const Gfx::Pen &p)
Sets the widget-local contour pen to p.
Command button with optional toggle, icon, and caption.
Definition: PushButton.h:77
Active painting session on a Forms paint surface.
Definition: PaintContext.h:51
virtual void onReleased()
Completes the click and emits clicked().
virtual void onPaintBackground(PaintContext &context, const Gfx::RectF &rect, const ButtonState &state)
Paints the button background layer.
void setAccentColor(const Gfx::Color &color)
Sets the widget-local accent color to color.
void setFontSlant(Gfx::Font::Slant slant)
Sets the widget-local font slant to slant.
Size with floating-point width and height.
Definition: Size.h:47
Point with floating-point X and Y coordinates.
Definition: Point.h:47
virtual void onPaintFrame(PaintContext &context, const Gfx::RectF &rect, const ButtonState &state)
Paints the button frame layer.
Reusable view used as application content.
Definition: Control.h:107
Metrics for a measured line of text.
Definition: TextMetrics.h:46
Binds a push button to the current style renderer.
Definition: ButtonStyler.h:334
Weight
Describes the requested font weight.
Definition: Font.h:61
Renders the look of a push button.
Definition: ButtonStyler.h:124
void setRenderer(ButtonRenderer *renderer)
Assigns renderer as the family renderer.
Names a flow direction.
Definition: Direction.h:52
const Gfx::Pen & contour() const
Returns the effective contour pen.
void setFontSize(std::size_t size)
Sets the widget-local font size to size.
const Gfx::Color & highlightColor() const
Returns the effective highlight color.
const Gfx::Brush & foreground() const
Returns the effective foreground brush.
virtual void onLayout(const Gfx::RectF &rect)
Places icon, caption, and mnemonic in rect.
Font request used for text drawing and measurement.
Definition: Font.h:56
virtual void onPaintIcon(PaintContext &context, const Gfx::RectF &rect, const Pixmap &picture, const Gfx::PointF &pos, const ButtonState &state)
Paints the button icon layer.
Interaction control for press, release, and click.
Definition: Button.h:68
void setFont(const Gfx::Font &font)
Sets the widget-local font to font.
virtual void onCanceled()
Restores the pressed state after an abandoned click.
Constraint used when measuring a control.
Definition: SizePolicy.h:48
Standard color type.
Definition: Color.h:47
virtual void onPaint(PaintContext &context, const Gfx::RectF &updateRect)
Paints background, frame, icon, caption, and mnemonic.
void setIcon(const Icon &icon, const Gfx::SizeF &iconSize)
Sets the icon shown beside the caption.
Unicode capable basic_string.
Definition: Api-String.h:44
Fill description for shapes and text.
Definition: Brush.h:145
Rect with floating-point coordinates.
Definition: Rect.h:47
Off-screen Forms paint surface.
Definition: Pixmap.h:130
PushButton()
Creates a command button.
const Gfx::Color & accentColor() const
Returns the effective accent color.
virtual void onRescaleEvent(const RescaleEvent &ev)
Invalidates a prepared icon after a scale change.
virtual void onPressed()
Presses the button, or toggles it when isToggle() is true.
void setPressed(bool pressed)
Sets whether the button is visually pressed.
bool isToggle() const
Returns true if the button stays pressed after a click.