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 
54 namespace Pt {
55 
56 namespace Forms {
57 
58 class PaintContext;
59 class Painter;
60 class Pixmap;
61 class TextBlock;
62 class PushButton;
63 class Menu;
64 class MenuItem;
65 class MenuBar;
66 class MenuBarItem;
67 class ProgressBar;
68 
69 template <typename T>
70 class FacetPtr
71 {
72  public:
73  FacetPtr(T* facet = 0)
74  : _facet(facet)
75  {
76  if( _facet )
77  _facet->ref();
78  }
79 
80  FacetPtr(const FacetPtr& ptr)
81  : _facet(ptr._facet)
82  {
83  if( _facet )
84  _facet->ref();
85  }
86 
87  ~FacetPtr()
88  {
89  if(_facet)
90  {
91  if( 0 == _facet->unref() )
92  delete _facet;
93  }
94  }
95 
96  FacetPtr& operator=(const FacetPtr& ptr)
97  {
98  if(this == &ptr)
99  return *this;
100 
101  if(_facet)
102  {
103  if( 0 == _facet->unref() )
104  delete _facet;
105  }
106 
107  _facet = ptr._facet;
108  if( _facet )
109  _facet->ref();
110 
111  return *this;
112  }
113 
114  void reset(T* facet = 0)
115  {
116  if (_facet == facet)
117  return;
118 
119  if(_facet)
120  {
121  if( 0 == _facet->unref() )
122  delete _facet;
123  }
124 
125  _facet = facet;
126  if( _facet )
127  _facet->ref();
128  }
129 
130  T* operator->() const
131  { return _facet; }
132 
133  T& operator*() const
134  { return *_facet; }
135 
136  bool operator! () const
137  { return _facet == 0; }
138 
139  operator bool () const
140  { return _facet != 0; }
141 
142  T* get()
143  { return _facet; }
144 
145  const T* get() const
146  { return _facet; }
147 
148  private:
149  T* _facet;
150 };
151 
154 class PT_FORMS_API Style
155 {
156  public:
157  class Facet : private NonCopyable
158  {
159  public:
160  explicit Facet(const std::type_info& ti, std::size_t refs = 0)
161  : _typeId(&ti)
162  , _refs(refs)
163  {}
164 
165  virtual ~Facet()
166  {}
167 
168  const std::type_info& typeId() const
169  {
170  return *_typeId;
171  }
172 
173  void ref()
174  {
175  ++_refs;
176  }
177 
178  std::size_t unref()
179  {
180  return --_refs;
181  }
182 
185  void reset(const StyleOptions& options)
186  {
187  onReset(options);
188  }
189 
190  protected:
193  virtual void onReset(const StyleOptions& /*options*/)
194  {
195  }
196 
197  private:
198  const std::type_info* _typeId;
199  std::size_t _refs;
200  };
201 
202  public:
205  Style();
206 
209  Style(const Style& style);
210 
213  virtual ~Style();
214 
217  Style& operator=(const Style& style);
218 
221  void assign(const Style& style);
222 
225  void set(Facet* facet);
226 
229  void reset(const StyleOptions& options);
230 
237  std::size_t generation() const
238  {
239  return _generation;
240  }
241 
242  template <typename FacetT>
243  FacetT* get() const
244  {
245  Facet* facet = find( typeid(FacetT) );
246  return static_cast<FacetT*>(facet);
247  }
248 
249  private:
250  Facet* find(const std::type_info& ti) const;
251 
252  private:
253  typedef std::map<TypeInfo, Facet*> FacetMap;
254  FacetMap _facets;
255  std::size_t _generation;
256 };
257 
258 } // namespace
259 
260 } // namespace
261 
262 #endif
Core module.
Definition: Allocator.h:33
void assign(const Style &style)
Replaces all facets with those from another style.
Style(const Style &style)
Constructs a style by copying another style.
Style()
Constructs an empty style.
Style for widgets.
Definition: Style.h:155
void set(Facet *facet)
Registers or replaces a facet.
Style & operator=(const Style &style)
Assigns the contents of another style.
Style options container.
Definition: StyleOptions.h:535
std::size_t generation() const
Returns the current style generation.
Definition: Style.h:237
virtual ~Style()
Destroys the style and releases all registered facets.
Protects derived classes from being copied.
Definition: NonCopyable.h:54
void reset(const StyleOptions &options)
Resets all registered facets to the given global style options.