Style.h
1/*
2 Copyright (C) 2016 Laurentiu-Gheorghe Crisan
3 Copyright (C) 2016 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_Style_h
32#define Pt_Forms_Style_h
33
34#include <Pt/Forms/Api.h>
35#include <Pt/Forms/Alignment.h>
36#include <Pt/Forms/Direction.h>
37#include <Pt/Forms/Spacing.h>
38#include <Pt/Forms/StyleOptions.h>
39#include <Pt/Forms/PaintSurface.h>
40#include <Pt/Gfx/Color.h>
41#include <Pt/Gfx/Brush.h>
42#include <Pt/Gfx/Pen.h>
43#include <Pt/Gfx/Rect.h>
44#include <Pt/Gfx/Font.h>
45#include <Pt/Gfx/FontMetrics.h>
46#include <Pt/Gfx/TextMetrics.h>
47#include <Pt/TypeInfo.h>
48#include <Pt/NonCopyable.h>
49#include <Pt/SmartPtr.h>
50
51#include <map>
52#include <cstddef>
53
54namespace Pt {
55
56namespace Forms {
57
58class PaintContext;
59class Painter;
60class Pixmap;
61class TextBlock;
62class PushButton;
63class Menu;
64class MenuItem;
65class MenuBar;
66class MenuBarItem;
67class ProgressBar;
68
77template <typename T>
79{
80 public:
83 FacetPtr(T* facet = 0)
84 : _facet(facet)
85 {
86 if( _facet )
87 _facet->ref();
88 }
89
92 FacetPtr(const FacetPtr& ptr)
93 : _facet(ptr._facet)
94 {
95 if( _facet )
96 _facet->ref();
97 }
98
102 {
103 if(_facet)
104 {
105 if( 0 == _facet->unref() )
106 delete _facet;
107 }
108 }
109
113 {
114 if(this == &ptr)
115 return *this;
116
117 if(_facet)
118 {
119 if( 0 == _facet->unref() )
120 delete _facet;
121 }
122
123 _facet = ptr._facet;
124 if( _facet )
125 _facet->ref();
126
127 return *this;
128 }
129
132 void reset(T* facet = 0)
133 {
134 if (_facet == facet)
135 return;
136
137 if(_facet)
138 {
139 if( 0 == _facet->unref() )
140 delete _facet;
141 }
142
143 _facet = facet;
144 if( _facet )
145 _facet->ref();
146 }
147
150 T* operator->() const
151 { return _facet; }
152
155 T& operator*() const
156 { return *_facet; }
157
160 bool operator! () const
161 { return _facet == 0; }
162
165 operator bool () const
166 { return _facet != 0; }
167
170 T* get()
171 { return _facet; }
172
175 const T* get() const
176 { return _facet; }
177
178 private:
179 T* _facet;
180};
181
202class PT_FORMS_API Style
203{
204 public:
210 class Facet : private NonCopyable
211 {
212 public:
215 explicit Facet(const std::type_info& ti, std::size_t refs = 0)
216 : _typeId(&ti)
217 , _refs(refs)
218 {}
219
222 virtual ~Facet()
223 {}
224
227 const std::type_info& typeId() const
228 {
229 return *_typeId;
230 }
231
234 void ref()
235 {
236 ++_refs;
237 }
238
241 std::size_t unref()
242 {
243 return --_refs;
244 }
245
248 void reset(const StyleOptions& options)
249 {
250 onReset(options);
251 }
252
253 protected:
261 virtual void onReset(const StyleOptions& /*options*/)
262 {
263 }
264
265 private:
266 const std::type_info* _typeId;
267 std::size_t _refs;
268 };
269
270 public:
274
277 Style(const Style& style);
278
281 virtual ~Style();
282
285 Style& operator=(const Style& style);
286
289 void assign(const Style& style);
290
296 void set(Facet* facet);
297
300 void reset(const StyleOptions& options);
301
306 std::size_t generation() const
307 {
308 return _generation;
309 }
310
313 template <typename FacetT>
314 FacetT* get() const
315 {
316 Facet* facet = find( typeid(FacetT) );
317 return static_cast<FacetT*>(facet);
318 }
319
320 private:
321 Facet* find(const std::type_info& ti) const;
322
323 private:
324 typedef std::map<TypeInfo, Facet*> FacetMap;
325 FacetMap _facets;
326 std::size_t _generation;
327};
328
329} // namespace
330
331} // namespace
332
333#endif
T * get()
Returns the facet, or 0.
Definition Style.h:170
T & operator*() const
Returns the facet.
Definition Style.h:155
FacetPtr(T *facet=0)
Holds facet, or 0.
Definition Style.h:83
~FacetPtr()
Destructor.
Definition Style.h:101
FacetPtr(const FacetPtr &ptr)
Shares the facet held by ptr.
Definition Style.h:92
bool operator!() const
Returns true if no facet is held.
Definition Style.h:160
T * operator->() const
Returns the facet.
Definition Style.h:150
FacetPtr & operator=(const FacetPtr &ptr)
Replaces the held facet with the facet from ptr.
Definition Style.h:112
const T * get() const
Returns the facet, or 0.
Definition Style.h:175
void reset(T *facet=0)
Replaces the held facet with facet, or 0.
Definition Style.h:132
Represents a top-level entry in a menu bar.
Definition MenuBarItem.h:54
Represents a horizontal bar of top-level menus.
Definition MenuBar.h:78
Represents a command that can be chosen from a menu.
Definition MenuItem.h:50
Represents a popup menu containing commands and nested menus.
Definition Menu.h:71
Active painting session on a Forms paint surface.
Definition PaintContext.h:51
Draws on a Forms paint surface or paint context.
Definition Painter.h:53
Off-screen Forms paint surface.
Definition Pixmap.h:130
Control that shows a value within a range.
Definition ProgressBar.h:70
Command button with optional toggle, icon, and caption.
Definition PushButton.h:77
Stores named appearance options.
Definition StyleOptions.h:646
Represents a type-tagged, reference-counted object stored by Style.
Definition Style.h:211
virtual void onReset(const StyleOptions &)
Resets this facet with options.
Definition Style.h:261
Facet(const std::type_info &ti, std::size_t refs=0)
Constructor.
Definition Style.h:215
std::size_t unref()
Removes one reference and returns the remaining count.
Definition Style.h:241
const std::type_info & typeId() const
Returns the dynamic type used as the registry key.
Definition Style.h:227
void reset(const StyleOptions &options)
Resets this facet with options.
Definition Style.h:248
virtual ~Facet()
Destructor.
Definition Style.h:222
void ref()
Adds one reference.
Definition Style.h:234
FacetT * get() const
Returns the facet of type FacetT, or 0.
Definition Style.h:314
void assign(const Style &style)
Replaces all facets with those from style.
Style(const Style &style)
Copies style.
Style()
Constructor.
std::size_t generation() const
Returns the current style generation.
Definition Style.h:306
Style & operator=(const Style &style)
Replaces the contents with a copy of style.
void reset(const StyleOptions &options)
Resets all registered facets with options.
void set(Facet *facet)
Adds or replaces a facet.
virtual ~Style()
Destructor.
Represents wrapped text as positioned lines.
Definition TextBlock.h:148
NonCopyable()
Default constructor.
Definition NonCopyable.h:63
Graphical user interfaces.
Definition ActivateEvent.h:40
Core module.
Definition Allocator.h:33