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/Icon.h>
35 #include <Pt/Forms/PixmapSurface.h>
36 #include <Pt/Gfx/Image.h>
37 #include <Pt/Gfx/Brush.h>
38 #include <Pt/Gfx/Pen.h>
39 #include <Pt/SmartPtr.h>
40 
41 namespace Pt {
42 
43 namespace Forms {
44 
45 class ButtonRenderer;
46 
47 class PT_FORMS_API PushButton : public Button
48 {
49  public:
50  typedef Button Base;
51 
52  enum Direction
53  {
54  Left,
55  Right,
56  Top,
57  Bottom
58  };
59 
60  public:
61  PushButton();
62 
63  virtual ~PushButton();
64 
65  bool isPressed() const;
66 
67  void setPressed(bool pressed);
68 
69  bool isToggle() const;
70 
71  void setToggle(bool toggle);
72 
73  void setIcon(const Icon& icon, const Gfx::SizeF& iconSize);
74 
75  bool isFlat() const;
76 
77  void setFlat(bool f);
78 
79  void setLayout(Direction d);
80 
81  public:
82  const Gfx::Brush& foreground() const;
83 
84  void setForeground(const Gfx::Brush& b);
85 
86  const Gfx::Pen& contour() const;
87 
88  void setContour(const Gfx::Pen& p);
89 
90  const Gfx::Color& accentColor() const;
91 
92  void setAccentColor(const Gfx::Color& color);
93 
94  const Gfx::Color& highlightColor() const;
95 
96  void setHighlightColor(const Gfx::Color& c);
97 
98  const Gfx::Color& textColor() const;
99 
100  void setTextColor(const Gfx::Color& color);
101 
102  const Gfx::Font& font() const;
103 
104  void setFont(const Gfx::Font& font);
105 
106  void setFontSize(std::size_t size);
107 
108  void setFontWeight(Gfx::Font::Weight weight);
109 
110  void setFontSlant(Gfx::Font::Slant slant);
111 
112  void setRenderer(ButtonRenderer* renderer);
113 
114  protected:
115  virtual void onPressed();
116 
117  virtual void onReleased();
118 
119  virtual void onCanceled();
120 
121  protected:
122  virtual void onSetStyleOptions(const StyleOptions& options);
123 
124  virtual Gfx::SizeF onMeasure(const SizePolicy& policy);
125 
126  virtual void onLayout(const Gfx::RectF& rect);
127 
128  virtual void onInvalidate();
129 
130  virtual void onPaint(PaintSurface& surface, const Gfx::RectF& updateRect);
131 
132  private:
133  void onIconChanged();
134 
135  Gfx::Font getFont() const;
136 
137  private:
138  enum FontOverride : unsigned
139  {
140  OverrideSize = 0x01,
141  OverrideWeight = 0x02,
142  OverrideSlant = 0x04,
143  OverrideAll = 0xFF
144  };
145 
146  private:
147  bool _isToggle;
148  bool _isPressed;
149  bool _isBeingToggled;
150  bool _isFlat;
151  Direction _direction;
152  Icon _icon;
153  Gfx::SizeF _iconSize;
154  Gfx::PointF _iconPos;
155  Gfx::PointF _textPos;
156  Gfx::TextMetrics _textMetrics;
157  Gfx::FontMetrics _fontMetrics;
158 
159  FacetPtr<ButtonRenderer> _renderer;
160  bool _hasRenderer;
161 
162  AutoPtr<Gfx::Brush> _foreground;
163  AutoPtr<Gfx::Pen> _contour;
164  AutoPtr<Gfx::Color> _accentColor;
165  AutoPtr<Gfx::Color> _highlightColor;
166  AutoPtr<Gfx::Color> _textColor;
167  Gfx::Font _customFont;
168  unsigned _fontOverride;
169  std::size_t _styleGeneration;
170 
171  Gfx::Brush _brush;
172  Gfx::Pen _pen;
173  Gfx::Pen _textPen;
174  Gfx::Font _font;
175  PixmapSurface _picture;
176 };
177 
178 } // namespace
179 
180 } // namespace
181 
182 #endif