StyleOptions.h
1 /*
2  Copyright (C) 2016-2026 Laurentiu-Gheorghe Crisan
3  Copyright (C) 2016-2026 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_StyleOptions_h
32 #define Pt_Forms_StyleOptions_h
33 
34 #include <Pt/Forms/Api.h>
35 #include <Pt/Gfx/Pen.h>
36 #include <Pt/Gfx/Brush.h>
37 #include <Pt/Gfx/Font.h>
38 #include <Pt/Gfx/Color.h>
39 #include <Pt/SmartPtr.h>
40 
41 #include <cstddef>
42 #include <stdexcept>
43 #include <string>
44 #include <typeinfo>
45 #include <vector>
46 
47 namespace Pt {
48 
49 namespace Forms {
50 
51 class StyleOptions;
52 
58 class PT_FORMS_API StyleOption
59 {
60  public:
61  virtual ~StyleOption();
62 
63  virtual StyleOption* clone() const = 0;
64 
65  virtual const std::type_info& typeId() const = 0;
66 
67  virtual const char* name() const = 0;
68 
69  virtual void bind(const StyleOptions* /*inherited*/)
70  {}
71 };
72 
76 {
77  public:
78  explicit BackgroundOption(const Gfx::Color& color)
79  : _value(color)
80  {}
81 
82  explicit BackgroundOption(const Gfx::Brush& brush)
83  : _value(brush)
84  {}
85 
86  const Gfx::Brush& value() const
87  { return _value; }
88 
89  virtual StyleOption* clone() const
90  { return new BackgroundOption(*this); }
91 
92  virtual const std::type_info& typeId() const
93  { return typeid(BackgroundOption); }
94 
95  virtual const char* name() const override
96  { return "background"; }
97 
98  private:
99  Gfx::Brush _value;
100 };
101 
105 {
106  public:
107  explicit ForegroundOption(const Gfx::Color& color)
108  : _value(color)
109  {}
110 
111  explicit ForegroundOption(const Gfx::Brush& brush)
112  : _value(brush)
113  {}
114 
115  const Gfx::Brush& value() const
116  { return _value; }
117 
118  virtual StyleOption* clone() const
119  { return new ForegroundOption(*this); }
120 
121  virtual const std::type_info& typeId() const
122  { return typeid(ForegroundOption); }
123 
124  virtual const char* name() const override
125  { return "foreground"; }
126 
127  private:
128  Gfx::Brush _value;
129 };
130 
134 {
135  public:
136  explicit ContourOption(const Gfx::Color& color)
137  : _value(color)
138  {}
139 
140  explicit ContourOption(const Gfx::Pen& pen)
141  : _value(pen)
142  {}
143 
144  const Gfx::Pen& value() const
145  { return _value; }
146 
147  virtual StyleOption* clone() const
148  { return new ContourOption(*this); }
149 
150  virtual const std::type_info& typeId() const
151  { return typeid(ContourOption); }
152 
153  virtual const char* name() const override
154  { return "contour"; }
155 
156  private:
157  Gfx::Pen _value;
158 };
159 
163 {
164  public:
165  explicit AccentColorOption(const Gfx::Color& color)
166  : _value(color)
167  {}
168 
169  const Gfx::Color& value() const
170  { return _value; }
171 
172  virtual StyleOption* clone() const
173  { return new AccentColorOption(*this); }
174 
175  virtual const std::type_info& typeId() const
176  { return typeid(AccentColorOption); }
177 
178  virtual const char* name() const override
179  { return "accentColor"; }
180 
181  private:
182  Gfx::Color _value;
183 };
184 
188 {
189  public:
190  explicit ViewBackgroundOption(const Gfx::Color& color)
191  : _value(color)
192  {}
193 
194  explicit ViewBackgroundOption(const Gfx::Brush& brush)
195  : _value(brush)
196  {}
197 
198  const Gfx::Brush& value() const
199  { return _value; }
200 
201  virtual StyleOption* clone() const
202  { return new ViewBackgroundOption(*this); }
203 
204  virtual const std::type_info& typeId() const
205  { return typeid(ViewBackgroundOption); }
206 
207  virtual const char* name() const override
208  { return "viewBackground"; }
209 
210  private:
211  Gfx::Brush _value;
212 };
213 
217 {
218  public:
219  explicit HighlightColorOption(const Gfx::Color& color)
220  : _value(color)
221  {}
222 
223  const Gfx::Color& value() const
224  { return _value; }
225 
226  virtual StyleOption* clone() const
227  { return new HighlightColorOption(*this); }
228 
229  virtual const std::type_info& typeId() const
230  { return typeid(HighlightColorOption); }
231 
232  virtual const char* name() const override
233  { return "highlightColor"; }
234 
235  private:
236  Gfx::Color _value;
237 };
238 
242 {
243  public:
244  explicit HoverBackgroundOption(const Gfx::Color& color)
245  : _value(color)
246  {}
247 
248  explicit HoverBackgroundOption(const Gfx::Brush& brush)
249  : _value(brush)
250  {}
251 
252  const Gfx::Brush& value() const
253  { return _value; }
254 
255  virtual StyleOption* clone() const
256  { return new HoverBackgroundOption(*this); }
257 
258  virtual const std::type_info& typeId() const
259  { return typeid(HoverBackgroundOption); }
260 
261  virtual const char* name() const override
262  { return "hoverBackground"; }
263 
264  private:
265  Gfx::Brush _value;
266 };
267 
271 {
272  public:
273  explicit TextBackgroundOption(const Gfx::Color& color)
274  : _value(color)
275  {}
276 
277  explicit TextBackgroundOption(const Gfx::Brush& brush)
278  : _value(brush)
279  {}
280 
281  const Gfx::Brush& value() const
282  { return _value; }
283 
284  virtual StyleOption* clone() const
285  { return new TextBackgroundOption(*this); }
286 
287  virtual const std::type_info& typeId() const
288  { return typeid(TextBackgroundOption); }
289 
290  virtual const char* name() const override
291  { return "textBackground"; }
292 
293  private:
294  Gfx::Brush _value;
295 };
296 
300 {
301  public:
302  explicit TextColorOption(const Gfx::Color& color)
303  : _value(color)
304  {}
305 
306  const Gfx::Color& value() const
307  { return _value; }
308 
309  virtual StyleOption* clone() const
310  { return new TextColorOption(*this); }
311 
312  virtual const std::type_info& typeId() const
313  { return typeid(TextColorOption); }
314 
315  virtual const char* name() const override
316  { return "textColor"; }
317 
318  private:
319  Gfx::Color _value;
320 };
321 
325 {
326  public:
327  explicit PlaceholderTextColorOption(const Gfx::Color& color)
328  : _value(color)
329  {}
330 
331  const Gfx::Color& value() const
332  { return _value; }
333 
334  virtual StyleOption* clone() const
335  { return new PlaceholderTextColorOption(*this); }
336 
337  virtual const std::type_info& typeId() const
338  { return typeid(PlaceholderTextColorOption); }
339 
340  virtual const char* name() const override
341  { return "placeholderTextColor"; }
342 
343  private:
344  Gfx::Color _value;
345 };
346 
350 {
351  public:
352  explicit HighlightedTextColorOption(const Gfx::Color& color)
353  : _value(color)
354  {}
355 
356  const Gfx::Color& value() const
357  { return _value; }
358 
359  virtual StyleOption* clone() const
360  { return new HighlightedTextColorOption(*this); }
361 
362  virtual const std::type_info& typeId() const
363  { return typeid(HighlightedTextColorOption); }
364 
365  virtual const char* name() const override
366  { return "highlightedTextColor"; }
367 
368  private:
369  Gfx::Color _value;
370 };
371 
375 {
376  public:
377  explicit AlternateViewBackgroundOption(const Gfx::Color& color)
378  : _value(color)
379  {}
380 
381  explicit AlternateViewBackgroundOption(const Gfx::Brush& brush)
382  : _value(brush)
383  {}
384 
385  const Gfx::Brush& value() const
386  { return _value; }
387 
388  virtual StyleOption* clone() const
389  { return new AlternateViewBackgroundOption(*this); }
390 
391  virtual const std::type_info& typeId() const
392  { return typeid(AlternateViewBackgroundOption); }
393 
394  virtual const char* name() const override
395  { return "alternateViewBackground"; }
396 
397  private:
398  Gfx::Brush _value;
399 };
400 
404 {
405  public:
406  explicit PopupBackgroundOption(const Gfx::Color& color)
407  : _value(color)
408  {}
409 
410  explicit PopupBackgroundOption(const Gfx::Brush& brush)
411  : _value(brush)
412  {}
413 
414  const Gfx::Brush& value() const
415  { return _value; }
416 
417  virtual StyleOption* clone() const
418  { return new PopupBackgroundOption(*this); }
419 
420  virtual const std::type_info& typeId() const
421  { return typeid(PopupBackgroundOption); }
422 
423  virtual const char* name() const override
424  { return "popupBackground"; }
425 
426  private:
427  Gfx::Brush _value;
428 };
429 
433 {
434  public:
435  explicit PopupTextColorOption(const Gfx::Color& color)
436  : _value(color)
437  {}
438 
439  const Gfx::Color& value() const
440  { return _value; }
441 
442  virtual StyleOption* clone() const
443  { return new PopupTextColorOption(*this); }
444 
445  virtual const std::type_info& typeId() const
446  { return typeid(PopupTextColorOption); }
447 
448  virtual const char* name() const override
449  { return "popupTextColor"; }
450 
451  private:
452  Gfx::Color _value;
453 };
454 
457 class PT_FORMS_API FontOption : public StyleOption
458 {
459  public:
463 
464  FontOption(const FontOption& o);
465 
466  FontOption& operator=(const FontOption& o);
467 
468  virtual StyleOption* clone() const;
469 
470  virtual const std::type_info& typeId() const;
471 
472  virtual const char* name() const override
473  { return "font"; }
474 
477  bool isSet() const;
478 
481  const Gfx::Font& value() const;
482 
485  void setFont(const Gfx::Font& font);
486 
489  void setSize(std::size_t size);
490 
494 
498 
499  virtual void bind(const StyleOptions* inherited);
500 
503  Gfx::Font getFont(const Gfx::Font& baseFont) const;
504 
505  private:
506  enum Override
507  {
508  All = 0x1,
509  Size = 0x2,
510  Weight = 0x4,
511  Slant = 0x8
512  };
513 
514  private:
515  AutoPtr<Gfx::Font> _font;
516  std::size_t _size;
517  Gfx::Font::Weight _weight;
518  Gfx::Font::Slant _slant;
519  Gfx::Font _resolvedFont;
520  unsigned _overrides;
521 };
522 
534 class PT_FORMS_API StyleOptions
535 {
536  public:
537  static const std::size_t InvalidGeneration = 0;
538 
539  public:
543 
547 
551 
555 
559 
562  std::size_t generation() const;
563 
568  bool hasOptions() const;
569 
572  bool isDefault(const StyleOptions& base) const;
573 
579  void bind(const StyleOptions* inherited);
580 
583  const StyleOptions* parent() const;
584 
587  template <typename T>
588  const T* findLocal() const;
589 
592  template <typename T>
593  const T* find() const;
594 
597  template <typename T>
598  const T& get() const;
599 
602  template <typename T>
603  void set(const T& option);
604 
607  template <typename T>
608  void reset();
609 
610  private:
611  bool hasLocalOptions() const;
612 
613  StyleOption* findOption(const std::type_info& ti) const;
614 
615  void replaceOption(StyleOption* option);
616 
617  void removeOption(const std::type_info& ti);
618 
619  void clearOptions();
620 
621  private:
622  std::size_t _generation;
623  std::size_t _boundGeneration;
624  const StyleOptions* _parent;
625  std::vector<StyleOption*> _options;
626 };
627 
628 
629 template <typename T>
630 const T* StyleOptions::findLocal() const
631 {
632  return static_cast<const T*>( findOption(typeid(T)) );
633 }
634 
635 
636 template <typename T>
637 const T* StyleOptions::find() const
638 {
639  const T* option = findLocal<T>();
640  if(option)
641  return option;
642 
643  if( _parent )
644  return _parent->find<T>();
645 
646  return 0;
647 }
648 
649 
650 template <typename T>
651 const T& StyleOptions::get() const
652 {
653  const T* option = find<T>();
654  if(option)
655  return *option;
656 
657  throw std::logic_error("invalid style option");
658 }
659 
660 
661 template <typename T>
662 void StyleOptions::set(const T& option)
663 {
664  StyleOption* localOption = option.clone();
665  localOption->bind(_parent);
666  replaceOption(localOption);
667 }
668 
669 
670 template <typename T>
672 {
673  removeOption(typeid(T));
674 }
675 
676 } // namespace
677 
678 } // namespace
679 
680 #endif
Core module.
Definition: Allocator.h:33
void set(const T &option)
Sets or replaces the local option of type T.
Definition: StyleOptions.h:662
Placeholder text color option.
Definition: StyleOptions.h:325
void setWeight(Gfx::Font::Weight weight)
Sets the local font weight override.
Attributes for the drawing of outlines.
Definition: Pen.h:54
Hover background brush option.
Definition: StyleOptions.h:242
void reset()
Removes the local option of type T if present.
Definition: StyleOptions.h:671
Contour pen option.
Definition: StyleOptions.h:134
StyleOptions & operator=(const StyleOptions &o)
Assigns style options.
Foreground brush option.
Definition: StyleOptions.h:105
const StyleOptions * parent() const
Returns the parent container that provides inherited options, or 0.
Alternate view background brush option.
Definition: StyleOptions.h:375
Slant
Describes the requested font slant.
Definition: Font.h:76
FontOption()
Constructs an empty font option.
Highlighted text color option.
Definition: StyleOptions.h:350
const Gfx::Font & value() const
Returns the effective font.
~StyleOptions()
Destructor.
const T * find() const
Returns the local or inherited option of type T, or 0 if absent.
Definition: StyleOptions.h:637
Policy based Auto pointer.
Definition: SmartPtr.h:291
const T & get() const
Returns the local or inherited option of type T.
Definition: StyleOptions.h:651
void bind(const StyleOptions *inherited)
Binds local options to inherited and materializes effective values.
static StyleOptions defaults()
Returns a complete container with the built-in default options.
Font option.
Definition: StyleOptions.h:458
Weight
Describes the requested font weight.
Definition: Font.h:61
void setFont(const Gfx::Font &font)
Replaces the complete local font override.
View background brush option.
Definition: StyleOptions.h:188
Text color option.
Definition: StyleOptions.h:300
bool isDefault(const StyleOptions &base) const
Returns true if no options override base.
Font request used for text drawing and measurement.
Definition: Font.h:56
void setSlant(Gfx::Font::Slant slant)
Sets the local font slant override.
Highlight color option.
Definition: StyleOptions.h:217
Style options container.
Definition: StyleOptions.h:535
const T * findLocal() const
Returns the local option of type T, or 0 if absent.
Definition: StyleOptions.h:630
Standard color type.
Definition: Color.h:47
bool hasOptions() const
Returns true if the container contains any local option.
StyleOptions()
Constructs an empty container.
Accent color option.
Definition: StyleOptions.h:163
Fill description for shapes and text.
Definition: Brush.h:145
StyleOptions(const StyleOptions &o)
Copy Constructor.
Background brush option.
Definition: StyleOptions.h:76
std::size_t generation() const
Returns the current change generation.
Gfx::Font getFont(const Gfx::Font &baseFont) const
Returns the local font overrides merged with baseFont.
Popup background brush option.
Definition: StyleOptions.h:404
bool isSet() const
Returns true if any font override is set.
Text background brush option.
Definition: StyleOptions.h:271
Popup text color option.
Definition: StyleOptions.h:433
Non-template base for a typed style option.
Definition: StyleOptions.h:59
void setSize(std::size_t size)
Sets the local font size override.