StyleOptions.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_StyleOptions_h
31 #define Pt_Forms_StyleOptions_h
32 
33 #include <Pt/Forms/Api.h>
34 #include <Pt/Gfx/Pen.h>
35 #include <Pt/Gfx/Brush.h>
36 #include <Pt/Gfx/Font.h>
37 #include <Pt/Gfx/Color.h>
38 #include <Pt/Any.h>
39 #include <Pt/NonCopyable.h>
40 #include <Pt/SmartPtr.h>
41 
42 #include <cstddef>
43 #include <string>
44 #include <typeinfo>
45 #include <vector>
46 
47 namespace Pt {
48 
49 namespace Forms {
50 
56 class PT_FORMS_API StyleOption
57 {
58  public:
59  virtual ~StyleOption()
60  {};
61 
63  const char* name() const
64  {
65  return _name.c_str();
66  }
67 
70  virtual const std::type_info& typeInfo() const = 0;
71 
74  virtual const Pt::Any& get() const = 0;
75 
78  virtual void set(const Pt::Any& v) = 0;
79 
80  protected:
81  explicit StyleOption(const char* name)
82  : _name(name)
83  {}
84 
85  private:
86  std::string _name;
87 };
88 
89 
95 template<typename T>
97 {
98  public:
99  BasicStyleOption(const char* name, const T& value)
100  : StyleOption(name)
101  , _value(value)
102  {}
103 
105  const T& value() const
106  {
107  return any_cast<const T&>(_value);
108  }
109 
111  void setValue(const T& v)
112  {
113  _value = Pt::Any(v);
114  }
115 
118  {
119  _value = o._value;
120  return *this;
121  }
122 
123  virtual const std::type_info& typeInfo() const
124  {
125  return typeid(T);
126  }
127 
128  virtual const Pt::Any& get() const
129  {
130  return _value;
131  }
132 
133  virtual void set(const Pt::Any& v)
134  {
135  setValue(any_cast<T>(v));
136  }
137 
138  private:
139  Pt::Any _value;
140 };
141 
142 
185 class PT_FORMS_API StyleOptions
186 {
187  public:
189  class Iterator
190  {
191  public:
192  explicit Iterator(StyleOption* const* p)
193  : _ptr(p)
194  {}
195 
196  const StyleOption& operator*() const
197  { return **_ptr; }
198 
199  const StyleOption* operator->() const
200  { return *_ptr; }
201 
202  Iterator& operator++()
203  {
204  ++_ptr;
205  return *this;
206  }
207 
208  bool operator!=(const Iterator& o) const
209  { return _ptr != o._ptr; }
210 
211  bool operator==(const Iterator& o) const
212  { return _ptr == o._ptr; }
213 
214  private:
215  StyleOption* const* _ptr;
216  };
217 
218  public:
219  explicit StyleOptions();
220 
221  StyleOptions(const StyleOptions& o);
222 
223  virtual ~StyleOptions();
224 
225  StyleOptions& operator=(const StyleOptions& o);
226 
229  std::size_t generation() const;
230 
231  const Gfx::Brush& background() const;
232 
233  void setBackground(const Gfx::Brush& b);
234 
235  const Gfx::Brush& foreground() const;
236 
237  void setForeground(const Gfx::Brush& c);
238 
239  const Gfx::Pen& contour() const;
240 
241  void setContour(const Gfx::Pen& p);
242 
243  const Gfx::Color& accentColor() const;
244 
245  void setAccentColor(const Gfx::Color& color);
246 
247  const Gfx::Brush& viewBackground() const;
248 
249  void setViewBackground(const Gfx::Brush& b);
250 
251  const Gfx::Color& highlightColor() const;
252 
253  void setHighlightColor(const Gfx::Color& c);
254 
255  const Gfx::Brush& hoverBackground() const;
256 
257  void setHoverBackground(const Gfx::Brush& b);
258 
259  const Gfx::Brush& textBackground() const;
260 
261  void setTextBackground(const Gfx::Brush& b);
262 
263  const Gfx::Color& textColor() const;
264 
265  void setTextColor(const Gfx::Color& c);
266 
267  const Gfx::Color& placeholderTextColor() const;
268 
269  void setPlaceholderTextColor(const Gfx::Color& c);
270 
271  const Gfx::Color& highlightedTextColor() const;
272 
273  void setHighlightedTextColor(const Gfx::Color& c);
274 
275  const Gfx::Brush& alternateViewBackground() const;
276 
277  void setAlternateViewBackground(const Gfx::Brush& b);
278 
279  const Gfx::Brush& popupBackground() const;
280 
281  void setPopupBackground(const Gfx::Brush& b);
282 
283  const Gfx::Color& popupTextColor() const;
284 
285  void setPopupTextColor(const Gfx::Color& c);
286 
287  const Gfx::Font& font() const;
288 
289  void setFont(const Gfx::Font& f);
290 
293  Iterator begin() const;
294 
297  Iterator end() const;
298 
301  const StyleOption* find(const char* name) const;
302 
305  void set(const char* name, const Pt::Any& value);
306 
307  protected:
311 
312  private:
313  void init();
314 
315  private:
316  std::size_t _generation;
317  std::vector<StyleOption*> _options;
318 
319  BasicStyleOption<Gfx::Brush> _background;
320  BasicStyleOption<Gfx::Brush> _foreground;
322  BasicStyleOption<Gfx::Color> _accentColor;
323  BasicStyleOption<Gfx::Brush> _viewBackground;
324  BasicStyleOption<Gfx::Color> _highlightColor;
325  BasicStyleOption<Gfx::Brush> _hoverBackground;
326  BasicStyleOption<Gfx::Brush> _textBackground;
327  BasicStyleOption<Gfx::Color> _textColor;
328  BasicStyleOption<Gfx::Color> _placeholderTextColor;
329  BasicStyleOption<Gfx::Color> _highlightedTextColor;
330  BasicStyleOption<Gfx::Brush> _alternateViewBackground;
331  BasicStyleOption<Gfx::Brush> _popupBackground;
332  BasicStyleOption<Gfx::Color> _popupTextColor;
334 };
335 
343 class PT_FORMS_API FontOption
344 {
345  public:
349 
352  bool hasOverride() const;
353 
356  const Gfx::Font* font() const;
357 
360  void setFont(const Gfx::Font& font);
361 
364  void setSize(std::size_t size);
365 
369 
373 
380  Gfx::Font getFont(const Gfx::Font& base) const;
381 
382  private:
383  enum Override
384  {
385  All = 0x1,
386  Size = 0x2,
387  Weight = 0x4,
388  Slant = 0x8
389  };
390 
391  private:
392  AutoPtr<Gfx::Font> _font;
393  unsigned _overrides;
394 };
395 
396 } // namespace
397 
398 } // namespace
399 
400 #endif
Core module.
Definition: Allocator.h:33
void setWeight(Gfx::Font::Weight weight)
Sets the local font weight override.
Iterator end() const
Returns an iterator past the last option.
Attributes for the drawing of outlines.
Definition: Pen.h:54
const char * name() const
Returns the option name.
Definition: StyleOptions.h:63
const Gfx::Font * font() const
Returns the local font override or 0 if none is set.
bool hasOverride() const
Returns true if any font override is set.
virtual void set(const Pt::Any &v)
Sets the value from an Any; throws std::bad_cast on type mismatch.
Definition: StyleOptions.h:133
Slant
Describes the requested font slant.
Definition: Font.h:76
FontOption()
Constructs an empty font option.
const T & value() const
Returns the stored value.
Definition: StyleOptions.h:105
virtual const Pt::Any & get() const
Returns the current value wrapped in an Any.
Definition: StyleOptions.h:128
virtual const std::type_info & typeInfo() const =0
Returns the dynamic type of the stored value.
void set(const char *name, const Pt::Any &value)
Sets an option by name from an Any value; no-op if name is unknown.
Gfx::Font getFont(const Gfx::Font &base) const
Resolves the effective font against the given base font.
Policy based Auto pointer.
Definition: SmartPtr.h:291
virtual const std::type_info & typeInfo() const
Returns the dynamic type of the stored value.
Definition: StyleOptions.h:123
BasicStyleOption & operator=(const BasicStyleOption &o)
Copies the value only.
Definition: StyleOptions.h:117
Typed style option that stores a value of type T.
Definition: StyleOptions.h:97
Composable font override slice for widget-local style options.
Definition: StyleOptions.h:344
Weight
Describes the requested font weight.
Definition: Font.h:61
void setFont(const Gfx::Font &font)
Replaces the complete local font override.
Iterator begin() const
Returns an iterator to the first option.
void registerOption(StyleOption *opt)
Registers a StyleOption into the polymorphic vector for iteration.
Font request used for text drawing and measurement.
Definition: Font.h:56
void setSlant(Gfx::Font::Slant slant)
Sets the local font slant override.
void setValue(const T &v)
Sets the value (does not bump generation).
Definition: StyleOptions.h:111
Stores the global theme option values shared across styles.
Definition: StyleOptions.h:186
virtual const Pt::Any & get() const =0
Returns the current value wrapped in an Any.
Contains an arbitrary type.
Definition: Any.h:64
virtual void set(const Pt::Any &v)=0
Sets the value from an Any; throws std::bad_cast on type mismatch.
Standard color type.
Definition: Color.h:47
const StyleOption * find(const char *name) const
Finds an option by name, or returns 0 if not found.
Fill description for shapes and text.
Definition: Brush.h:145
std::size_t generation() const
Returns the current change generation.
Forward iterator over all StyleOption entries.
Definition: StyleOptions.h:190
Non-template base for a single named style option.
Definition: StyleOptions.h:57
void setSize(std::size_t size)
Sets the local font size override.