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
47namespace Pt {
48
49namespace Forms {
50
51class StyleOptions;
52
62class PT_FORMS_API StyleOption
63{
64 public:
67 virtual ~StyleOption();
68
71 virtual StyleOption* clone() const = 0;
72
75 virtual const std::type_info& typeId() const = 0;
76
79 virtual const char* name() const = 0;
80
86 virtual void bind(const StyleOptions* /*inherited*/)
87 {}
88};
89
94class BackgroundOption : public StyleOption
95{
96 public:
97 explicit BackgroundOption(const Gfx::Color& color)
98 : _value(color)
99 {}
100
101 explicit BackgroundOption(const Gfx::Brush& brush)
102 : _value(brush)
103 {}
104
105 const Gfx::Brush& value() const
106 { return _value; }
107
108 virtual StyleOption* clone() const
109 { return new BackgroundOption(*this); }
110
111 virtual const std::type_info& typeId() const
112 { return typeid(BackgroundOption); }
113
114 virtual const char* name() const override
115 { return "background"; }
116
117 private:
118 Gfx::Brush _value;
119};
120
125class ForegroundOption : public StyleOption
126{
127 public:
128 explicit ForegroundOption(const Gfx::Color& color)
129 : _value(color)
130 {}
131
132 explicit ForegroundOption(const Gfx::Brush& brush)
133 : _value(brush)
134 {}
135
136 const Gfx::Brush& value() const
137 { return _value; }
138
139 virtual StyleOption* clone() const
140 { return new ForegroundOption(*this); }
141
142 virtual const std::type_info& typeId() const
143 { return typeid(ForegroundOption); }
144
145 virtual const char* name() const override
146 { return "foreground"; }
147
148 private:
149 Gfx::Brush _value;
150};
151
156class ContourOption : public StyleOption
157{
158 public:
159 explicit ContourOption(const Gfx::Color& color)
160 : _value(color)
161 {}
162
163 explicit ContourOption(const Gfx::Pen& pen)
164 : _value(pen)
165 {}
166
167 const Gfx::Pen& value() const
168 { return _value; }
169
170 virtual StyleOption* clone() const
171 { return new ContourOption(*this); }
172
173 virtual const std::type_info& typeId() const
174 { return typeid(ContourOption); }
175
176 virtual const char* name() const override
177 { return "contour"; }
178
179 private:
180 Gfx::Pen _value;
181};
182
187class AccentColorOption : public StyleOption
188{
189 public:
190 explicit AccentColorOption(const Gfx::Color& color)
191 : _value(color)
192 {}
193
194 const Gfx::Color& value() const
195 { return _value; }
196
197 virtual StyleOption* clone() const
198 { return new AccentColorOption(*this); }
199
200 virtual const std::type_info& typeId() const
201 { return typeid(AccentColorOption); }
202
203 virtual const char* name() const override
204 { return "accentColor"; }
205
206 private:
207 Gfx::Color _value;
208};
209
214class ViewBackgroundOption : public StyleOption
215{
216 public:
217 explicit ViewBackgroundOption(const Gfx::Color& color)
218 : _value(color)
219 {}
220
221 explicit ViewBackgroundOption(const Gfx::Brush& brush)
222 : _value(brush)
223 {}
224
225 const Gfx::Brush& value() const
226 { return _value; }
227
228 virtual StyleOption* clone() const
229 { return new ViewBackgroundOption(*this); }
230
231 virtual const std::type_info& typeId() const
232 { return typeid(ViewBackgroundOption); }
233
234 virtual const char* name() const override
235 { return "viewBackground"; }
236
237 private:
238 Gfx::Brush _value;
239};
240
245class HighlightColorOption : public StyleOption
246{
247 public:
248 explicit HighlightColorOption(const Gfx::Color& color)
249 : _value(color)
250 {}
251
252 const Gfx::Color& value() const
253 { return _value; }
254
255 virtual StyleOption* clone() const
256 { return new HighlightColorOption(*this); }
257
258 virtual const std::type_info& typeId() const
259 { return typeid(HighlightColorOption); }
260
261 virtual const char* name() const override
262 { return "highlightColor"; }
263
264 private:
265 Gfx::Color _value;
266};
267
272class HoverBackgroundOption : public StyleOption
273{
274 public:
275 explicit HoverBackgroundOption(const Gfx::Color& color)
276 : _value(color)
277 {}
278
279 explicit HoverBackgroundOption(const Gfx::Brush& brush)
280 : _value(brush)
281 {}
282
283 const Gfx::Brush& value() const
284 { return _value; }
285
286 virtual StyleOption* clone() const
287 { return new HoverBackgroundOption(*this); }
288
289 virtual const std::type_info& typeId() const
290 { return typeid(HoverBackgroundOption); }
291
292 virtual const char* name() const override
293 { return "hoverBackground"; }
294
295 private:
296 Gfx::Brush _value;
297};
298
303class TextBackgroundOption : public StyleOption
304{
305 public:
306 explicit TextBackgroundOption(const Gfx::Color& color)
307 : _value(color)
308 {}
309
310 explicit TextBackgroundOption(const Gfx::Brush& brush)
311 : _value(brush)
312 {}
313
314 const Gfx::Brush& value() const
315 { return _value; }
316
317 virtual StyleOption* clone() const
318 { return new TextBackgroundOption(*this); }
319
320 virtual const std::type_info& typeId() const
321 { return typeid(TextBackgroundOption); }
322
323 virtual const char* name() const override
324 { return "textBackground"; }
325
326 private:
327 Gfx::Brush _value;
328};
329
334class TextColorOption : public StyleOption
335{
336 public:
337 explicit TextColorOption(const Gfx::Color& color)
338 : _value(color)
339 {}
340
341 const Gfx::Color& value() const
342 { return _value; }
343
344 virtual StyleOption* clone() const
345 { return new TextColorOption(*this); }
346
347 virtual const std::type_info& typeId() const
348 { return typeid(TextColorOption); }
349
350 virtual const char* name() const override
351 { return "textColor"; }
352
353 private:
354 Gfx::Color _value;
355};
356
361class PlaceholderTextColorOption : public StyleOption
362{
363 public:
364 explicit PlaceholderTextColorOption(const Gfx::Color& color)
365 : _value(color)
366 {}
367
368 const Gfx::Color& value() const
369 { return _value; }
370
371 virtual StyleOption* clone() const
372 { return new PlaceholderTextColorOption(*this); }
373
374 virtual const std::type_info& typeId() const
375 { return typeid(PlaceholderTextColorOption); }
376
377 virtual const char* name() const override
378 { return "placeholderTextColor"; }
379
380 private:
381 Gfx::Color _value;
382};
383
388class HighlightedTextColorOption : public StyleOption
389{
390 public:
391 explicit HighlightedTextColorOption(const Gfx::Color& color)
392 : _value(color)
393 {}
394
395 const Gfx::Color& value() const
396 { return _value; }
397
398 virtual StyleOption* clone() const
399 { return new HighlightedTextColorOption(*this); }
400
401 virtual const std::type_info& typeId() const
402 { return typeid(HighlightedTextColorOption); }
403
404 virtual const char* name() const override
405 { return "highlightedTextColor"; }
406
407 private:
408 Gfx::Color _value;
409};
410
415class AlternateViewBackgroundOption : public StyleOption
416{
417 public:
418 explicit AlternateViewBackgroundOption(const Gfx::Color& color)
419 : _value(color)
420 {}
421
422 explicit AlternateViewBackgroundOption(const Gfx::Brush& brush)
423 : _value(brush)
424 {}
425
426 const Gfx::Brush& value() const
427 { return _value; }
428
429 virtual StyleOption* clone() const
430 { return new AlternateViewBackgroundOption(*this); }
431
432 virtual const std::type_info& typeId() const
433 { return typeid(AlternateViewBackgroundOption); }
434
435 virtual const char* name() const override
436 { return "alternateViewBackground"; }
437
438 private:
439 Gfx::Brush _value;
440};
441
446class PopupBackgroundOption : public StyleOption
447{
448 public:
449 explicit PopupBackgroundOption(const Gfx::Color& color)
450 : _value(color)
451 {}
452
453 explicit PopupBackgroundOption(const Gfx::Brush& brush)
454 : _value(brush)
455 {}
456
457 const Gfx::Brush& value() const
458 { return _value; }
459
460 virtual StyleOption* clone() const
461 { return new PopupBackgroundOption(*this); }
462
463 virtual const std::type_info& typeId() const
464 { return typeid(PopupBackgroundOption); }
465
466 virtual const char* name() const override
467 { return "popupBackground"; }
468
469 private:
470 Gfx::Brush _value;
471};
472
477class PopupTextColorOption : public StyleOption
478{
479 public:
480 explicit PopupTextColorOption(const Gfx::Color& color)
481 : _value(color)
482 {}
483
484 const Gfx::Color& value() const
485 { return _value; }
486
487 virtual StyleOption* clone() const
488 { return new PopupTextColorOption(*this); }
489
490 virtual const std::type_info& typeId() const
491 { return typeid(PopupTextColorOption); }
492
493 virtual const char* name() const override
494 { return "popupTextColor"; }
495
496 private:
497 Gfx::Color _value;
498};
499
511class PT_FORMS_API FontOption : public StyleOption
512{
513 public:
517
521
525
528 virtual StyleOption* clone() const;
529
532 virtual const std::type_info& typeId() const;
533
536 virtual const char* name() const override
537 { return "font"; }
538
541 bool isSet() const;
542
545 const Gfx::Font& value() const;
546
549 void setFont(const Gfx::Font& font);
550
553 void setSize(std::size_t size);
554
558
562
565 virtual void bind(const StyleOptions* inherited);
566
569 Gfx::Font getFont(const Gfx::Font& baseFont) const;
570
571 private:
572 enum Override
573 {
574 All = 0x1,
575 Size = 0x2,
576 Weight = 0x4,
577 Slant = 0x8
578 };
579
580 private:
581 AutoPtr<Gfx::Font> _font;
582 std::size_t _size;
583 Gfx::Font::Weight _weight;
584 Gfx::Font::Slant _slant;
585 Gfx::Font _resolvedFont;
586 unsigned _overrides;
587};
588
645class PT_FORMS_API StyleOptions
646{
647 public:
650 static const std::size_t InvalidGeneration = 0;
651
652 public:
656
660
664
668
672
678 std::size_t generation() const;
679
682 bool hasOptions() const;
683
686 bool isDefault(const StyleOptions& base) const;
687
693 void bind(const StyleOptions* inherited);
694
697 const StyleOptions* parent() const;
698
701 template <typename T>
702 const T* findLocal() const;
703
706 template <typename T>
707 const T* find() const;
708
713 template <typename T>
714 const T& get() const;
715
718 template <typename T>
719 void set(const T& option);
720
723 template <typename T>
724 void reset();
725
726 private:
727 bool hasLocalOptions() const;
728
729 StyleOption* findOption(const std::type_info& ti) const;
730
731 void replaceOption(StyleOption* option);
732
733 void removeOption(const std::type_info& ti);
734
735 void clearOptions();
736
737 private:
738 std::size_t _generation;
739 std::size_t _boundGeneration;
740 const StyleOptions* _parent;
741 std::vector<StyleOption*> _options;
742};
743
744
745template <typename T>
747{
748 return static_cast<const T*>( findOption(typeid(T)) );
749}
750
751
752template <typename T>
753const T* StyleOptions::find() const
754{
755 const T* option = findLocal<T>();
756 if(option)
757 return option;
758
759 if( _parent )
760 return _parent->find<T>();
761
762 return 0;
763}
764
765
766template <typename T>
767const T& StyleOptions::get() const
768{
769 const T* option = find<T>();
770 if(option)
771 return *option;
772
773 throw std::logic_error("invalid style option");
774}
775
776
777template <typename T>
778void StyleOptions::set(const T& option)
779{
780 StyleOption* localOption = option.clone();
781 localOption->bind(_parent);
782 replaceOption(localOption);
783}
784
785
786template <typename T>
788{
789 removeOption(typeid(T));
790}
791
792} // namespace
793
794} // namespace
795
796#endif
Policy based Auto pointer.
Definition SmartPtr.h:291
virtual const std::type_info & typeId() const
Returns the option class used as the lookup key.
Definition StyleOptions.h:200
virtual const char * name() const override
Returns the option name.
Definition StyleOptions.h:203
virtual StyleOption * clone() const
Returns a copy of this option.
Definition StyleOptions.h:197
virtual const std::type_info & typeId() const
Returns the option class used as the lookup key.
Definition StyleOptions.h:432
virtual const char * name() const override
Returns the option name.
Definition StyleOptions.h:435
virtual StyleOption * clone() const
Returns a copy of this option.
Definition StyleOptions.h:429
virtual const std::type_info & typeId() const
Returns the option class used as the lookup key.
Definition StyleOptions.h:111
virtual const char * name() const override
Returns the option name.
Definition StyleOptions.h:114
virtual StyleOption * clone() const
Returns a copy of this option.
Definition StyleOptions.h:108
virtual const std::type_info & typeId() const
Returns the option class used as the lookup key.
Definition StyleOptions.h:173
virtual const char * name() const override
Returns the option name.
Definition StyleOptions.h:176
virtual StyleOption * clone() const
Returns a copy of this option.
Definition StyleOptions.h:170
void setSlant(Gfx::Font::Slant slant)
Sets the local font slant override.
FontOption & operator=(const FontOption &o)
Assigns o.
const Gfx::Font & value() const
Returns the effective font after bind().
bool isSet() const
Returns true if any font override is set.
void setSize(std::size_t size)
Sets the local font size override.
FontOption()
Constructs an empty font option.
virtual const std::type_info & typeId() const
Returns the option class used as the lookup key.
void setWeight(Gfx::Font::Weight weight)
Sets the local font weight override.
virtual void bind(const StyleOptions *inherited)
Merges this option with the inherited font from inherited.
FontOption(const FontOption &o)
Copies o.
void setFont(const Gfx::Font &font)
Replaces the complete local font override.
virtual const char * name() const override
Returns the option name.
Definition StyleOptions.h:536
Gfx::Font getFont(const Gfx::Font &baseFont) const
Returns the local font overrides merged with baseFont.
virtual StyleOption * clone() const
Returns a copy of this option.
virtual const std::type_info & typeId() const
Returns the option class used as the lookup key.
Definition StyleOptions.h:142
virtual const char * name() const override
Returns the option name.
Definition StyleOptions.h:145
virtual StyleOption * clone() const
Returns a copy of this option.
Definition StyleOptions.h:139
virtual const std::type_info & typeId() const
Returns the option class used as the lookup key.
Definition StyleOptions.h:258
virtual const char * name() const override
Returns the option name.
Definition StyleOptions.h:261
virtual StyleOption * clone() const
Returns a copy of this option.
Definition StyleOptions.h:255
virtual const std::type_info & typeId() const
Returns the option class used as the lookup key.
Definition StyleOptions.h:401
virtual const char * name() const override
Returns the option name.
Definition StyleOptions.h:404
virtual StyleOption * clone() const
Returns a copy of this option.
Definition StyleOptions.h:398
virtual const std::type_info & typeId() const
Returns the option class used as the lookup key.
Definition StyleOptions.h:289
virtual const char * name() const override
Returns the option name.
Definition StyleOptions.h:292
virtual StyleOption * clone() const
Returns a copy of this option.
Definition StyleOptions.h:286
virtual const std::type_info & typeId() const
Returns the option class used as the lookup key.
Definition StyleOptions.h:374
virtual const char * name() const override
Returns the option name.
Definition StyleOptions.h:377
virtual StyleOption * clone() const
Returns a copy of this option.
Definition StyleOptions.h:371
virtual const std::type_info & typeId() const
Returns the option class used as the lookup key.
Definition StyleOptions.h:463
virtual const char * name() const override
Returns the option name.
Definition StyleOptions.h:466
virtual StyleOption * clone() const
Returns a copy of this option.
Definition StyleOptions.h:460
virtual const std::type_info & typeId() const
Returns the option class used as the lookup key.
Definition StyleOptions.h:490
virtual const char * name() const override
Returns the option name.
Definition StyleOptions.h:493
virtual StyleOption * clone() const
Returns a copy of this option.
Definition StyleOptions.h:487
Defines the base type of a named appearance option.
Definition StyleOptions.h:63
virtual StyleOption * clone() const =0
Returns a copy of this option.
virtual ~StyleOption()
Destructor.
virtual const char * name() const =0
Returns the option name.
virtual const std::type_info & typeId() const =0
Returns the option class used as the lookup key.
virtual void bind(const StyleOptions *)
Materializes this option against inherited.
Definition StyleOptions.h:86
Stores named appearance options.
Definition StyleOptions.h:646
const T * findLocal() const
Returns the local option of type T, or 0.
Definition StyleOptions.h:746
StyleOptions()
Constructs an empty options object.
const StyleOptions * parent() const
Returns the bound parent options, or 0.
const T * find() const
Returns the local or inherited option of type T, or 0.
Definition StyleOptions.h:753
static const std::size_t InvalidGeneration
Invalid generation value.
Definition StyleOptions.h:650
bool hasOptions() const
Returns true if this object contains any local option.
const T & get() const
Returns the local or inherited option of type T.
Definition StyleOptions.h:767
static StyleOptions defaults()
Returns options filled with the built-in default values.
void set(const T &option)
Adds or replaces the local option of type T.
Definition StyleOptions.h:778
void bind(const StyleOptions *inherited)
Binds this overlay to the parent options inherited.
StyleOptions & operator=(const StyleOptions &o)
Assigns o.
void reset()
Removes the local option of type T if present.
Definition StyleOptions.h:787
std::size_t generation() const
Returns the current change generation.
bool isDefault(const StyleOptions &base) const
Returns true if this object does not override base.
StyleOptions(const StyleOptions &o)
Copies o.
~StyleOptions()
Destructor.
virtual const std::type_info & typeId() const
Returns the option class used as the lookup key.
Definition StyleOptions.h:320
virtual const char * name() const override
Returns the option name.
Definition StyleOptions.h:323
virtual StyleOption * clone() const
Returns a copy of this option.
Definition StyleOptions.h:317
virtual const std::type_info & typeId() const
Returns the option class used as the lookup key.
Definition StyleOptions.h:347
virtual const char * name() const override
Returns the option name.
Definition StyleOptions.h:350
virtual StyleOption * clone() const
Returns a copy of this option.
Definition StyleOptions.h:344
virtual const std::type_info & typeId() const
Returns the option class used as the lookup key.
Definition StyleOptions.h:231
virtual const char * name() const override
Returns the option name.
Definition StyleOptions.h:234
virtual StyleOption * clone() const
Returns a copy of this option.
Definition StyleOptions.h:228
Fill color, gradient or texture.
Definition Brush.h:151
8-bit ARGB color.
Definition Color.h:65
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
Graphical user interfaces.
Definition ActivateEvent.h:40
Core module.
Definition Allocator.h:33