View.h
1 /* Copyright (C) 2015 Marc Boris Duerner
2  Copyright (C) 2015 Laurentiu-Gheorghe Crisan
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, MA
27  02110-1301 USA
28 */
29 
30 #ifndef PT_FORMS_VIEW_H
31 #define PT_FORMS_VIEW_H
32 
33 #include <Pt/Forms/Api.h>
34 #include <Pt/Forms/Widget.h>
35 #include <Pt/Forms/Pixmap.h>
36 #include <Pt/Forms/PaintSurface.h>
37 #include <Pt/Forms/Style.h>
38 
39 #include <Pt/Gfx/Canvas.h>
40 #include <Pt/Gfx/PaintSurface.h>
41 #include <Pt/Gfx/Point.h>
42 #include <Pt/Gfx/Size.h>
43 #include <Pt/Gfx/Rect.h>
44 
45 #include <cassert>
46 
47 namespace Pt {
48 
49 namespace Forms {
50 
51 class Control;
52 
53 class PT_FORMS_API View : public Widget
54 {
55  friend class Control;
56 
57  typedef Widget Base;
58 
59  public:
60  enum FocusPolicy
61  {
62  NoFocus,
63  AcceptFocus,
64  KeepFocus
65  };
66 
67  protected:
68  View();
69 
70  public:
71  virtual ~View();
72 
73  Gfx::PointF toControl(const Control& control,
74  const Gfx::PointF& pos) const;
75 
76  Gfx::PointF fromControl(const Control& control,
77  const Gfx::PointF& pos) const;
78 
79  void setStyleOptions(const StyleOptions& opts);
80 
81  PaintSurface& surface();
82 
83  const PaintSurface& surface() const;
84 
85  void setSurface(PaintSurface* surface,
86  const Gfx::PointF& pos = Gfx::PointF() );
87 
88  protected:
89  virtual void onSetSurface(PaintSurface* surface,
90  const Gfx::PointF& pos);
91 
92  virtual void onPaint(PaintSurface& surface,
93  const Gfx::RectF& rect);
94 
95  protected:
96  virtual void onAttach(Control& control);
97 
98  virtual void onDetach(Control& control);
99 
100  virtual void onInit(Control& control);
101 
102  virtual void onRelease(Control& control);
103 
104  virtual Gfx::PointF onToControl(const Control& control,
105  const Gfx::PointF& pos) const;
106 
107  virtual Gfx::PointF onFromControl(const Control& control,
108  const Gfx::PointF& pos) const;
109 
110  virtual void onSetStyleOptions(const StyleOptions& o);
111 
112  protected:
113  virtual void onRepaintRequest(Control& control, const Gfx::RectF& rect);
114 
115  virtual void onRelayoutRequest(Control& control);
116 
117  virtual void onEnableRequest(Control& control, bool isEnable);
118 
119  virtual void onActivateRequest(Control& control, bool active);
120 
121  virtual void onShowRequest(Control& control, bool isShown);
122 
123  virtual void onMoveRequest(Control& control, const Gfx::PointF& pos);
124 
125  virtual void onResizeRequest(Control& control, const Gfx::SizeF& size);
126 
127  virtual void onRaiseRequest(Control& control);
128 
129  //
130  // Widget
131  //
132  protected:
133  virtual void onConnect(Screen& screen);
134 
135  virtual void onDisconnect();
136 
137 
138  virtual void onPaintEvent(const PaintEvent& ev) override;
139 
140  virtual void onMoveEvent(const MoveEvent& ev) override;
141 
142  virtual void onResizeEvent(const ResizeEvent& ev) override;
143 
144  private:
145  class ViewSurface* _surface;
146 };
147 
150 class ViewSurface : public PaintSurface
151 {
152  public:
153  explicit ViewSurface(View& view)
154  : _view(&view)
155  , _surface(0)
156  {
157  }
158 
159  explicit ViewSurface(Widget& w)
160  : _view(&w)
161  , _surface(0)
162  {
163  }
164 
165  PaintSurface* surface()
166  {
167  return _surface;
168  }
169 
170  const Gfx::PointF& position() const
171  {
172  return _position;
173  }
174 
175  void setSurface(PaintSurface* surface,
176  const Gfx::PointF& pos)
177  {
178  _surface = surface;
179  _position = pos;
180  }
181 
182  protected:
183  virtual void onDrawPixmap(const Gfx::PointF& to,
184  const Pixmap& pm,
185  const Gfx::Paint& paint,
186  const Gfx::RectF* rect) override;
187 
188  virtual void onDrawPixmap(Gfx::Canvas& canvas,
189  const Gfx::PointF& to,
190  const Pixmap& pm,
191  const Gfx::RectF* rect) override;
192 
193  protected:
194  virtual const Gfx::ImageFormat& onGetFormat() const override
195  {
196  if (_surface )
197  return _surface->format();
198 
199  return Gfx::ImageFormat::argb32();
200  }
201 
202  const Gfx::SizeF& onGetSize() const
203  {
204  return _view->size();
205  }
206 
207  virtual const Gfx::Scaling& onGetScaling() const override
208  {
209  return _view->scaling();
210  }
211 
212  virtual Gfx::Canvas* onGetCanvas(Gfx::Canvas* reuse) override
213  {
214  Gfx::Canvas* canvas = _surface ? _surface->getCanvas(reuse)
215  : 0;
216  if( ! canvas )
217  return canvas;
218 
219  Gfx::RectF region = canvas->region();
220  region.shift( _position.x(), _position.y() );
221  region.setSize( _view->size() );
222 
223  canvas->setRegion(region);
224  return canvas;
225  }
226 
227  virtual Gfx::Canvas* onCreateCanvas(Gfx::Canvas* reuse) override
228  {
229  assert(false && "ViewSurface does not create its own canvas");
230  return 0;
231  }
232 
233  virtual void onReleaseCanvas() override
234  {
235  // context is released by parent surface
236  }
237 
238  virtual void onSync() override
239  {
240  // sync is done by parent surface
241  }
242 
243  virtual void onFinish() override
244  {
245  // sync is done by parent surface
246  }
247 
248  private:
249  Widget* _view;
250  PaintSurface* _surface;
251  Gfx::PointF _position;
252 };
253 
254 } // namespace
255 
256 } // namespace
257 
258 #endif // include guard