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 class PaintContext;
53 
65 class PT_FORMS_API View : public Widget
66 {
67  friend class Control;
68 
69  typedef Widget Base;
70 
71  public:
72  enum FocusPolicy
73  {
74  NoFocus,
75  AcceptFocus,
76  KeepFocus
77  };
78 
79  protected:
80  View();
81 
82  public:
83  virtual ~View();
84 
85  Gfx::PointF toControl(const Control& control,
86  const Gfx::PointF& pos) const;
87 
88  Gfx::PointF fromControl(const Control& control,
89  const Gfx::PointF& pos) const;
90 
91  PaintSurface& surface();
92 
93  const PaintSurface& surface() const;
94 
95  void setSurface(PaintSurface* surface,
96  const Gfx::PointF& pos = Gfx::PointF() );
97 
98  protected:
99  virtual void onSetSurface(PaintSurface* surface,
100  const Gfx::PointF& pos);
101 
102  virtual void onPaint(PaintContext& context,
103  const Gfx::RectF& rect);
104 
105  protected:
106  virtual void onAttach(Control& control);
107 
108  virtual void onDetach(Control& control);
109 
110  virtual void onInit(Control& control);
111 
112  virtual void onRelease(Control& control);
113 
114  virtual Gfx::PointF onToControl(const Control& control,
115  const Gfx::PointF& pos) const;
116 
117  virtual Gfx::PointF onFromControl(const Control& control,
118  const Gfx::PointF& pos) const;
119 
120  protected:
121  virtual void onRepaintRequest(Control& control, const Gfx::RectF& rect);
122 
123  virtual void onRelayoutRequest(Control& control);
124 
125  virtual void onEnableRequest(Control& control, bool isEnable);
126 
127  virtual void onActivateRequest(Control& control, bool active);
128 
129  virtual void onShowRequest(Control& control, bool isShown);
130 
131  virtual void onMoveRequest(Control& control, const Gfx::PointF& pos);
132 
133  virtual void onResizeRequest(Control& control, const Gfx::SizeF& size);
134 
135  virtual void onRaiseRequest(Control& control);
136 
137  //
138  // Widget
139  //
140  protected:
141  virtual void onConnect(Screen& screen);
142 
143  virtual void onDisconnect();
144 
145 
146  virtual void onPaintEvent(const PaintEvent& ev) override;
147 
148  virtual void onMoveEvent(const MoveEvent& ev) override;
149 
150  virtual void onResizeEvent(const ResizeEvent& ev) override;
151 
152  private:
153  class ViewSurface* _surface;
154 };
155 
158 class ViewSurface : public PaintSurface
159 {
160  public:
161  explicit ViewSurface(View& view)
162  : _view(&view)
163  , _surface(0)
164  {
165  }
166 
167  explicit ViewSurface(Widget& w)
168  : _view(&w)
169  , _surface(0)
170  {
171  }
172 
173  PaintSurface* surface()
174  {
175  return _surface;
176  }
177 
178  const Gfx::PointF& position() const
179  {
180  return _position;
181  }
182 
183  void setSurface(PaintSurface* surface,
184  const Gfx::PointF& pos)
185  {
186  _surface = surface;
187  _position = pos;
188  }
189 
190  protected:
191  virtual void onDrawPixmap(Gfx::Canvas& canvas,
192  const Gfx::PointF& to,
193  const Pixmap& pm,
194  const Gfx::RectF* rect) override;
195 
196  protected:
197  virtual const Gfx::ImageFormat& onGetFormat() const override
198  {
199  if (_surface )
200  return _surface->format();
201 
202  return Gfx::ImageFormat::argb32();
203  }
204 
205  const Gfx::SizeF& onGetSize() const
206  {
207  return _view->size();
208  }
209 
210  virtual const Gfx::Scaling& onGetScaling() const override
211  {
212  return _view->scaling();
213  }
214 
215  virtual Gfx::Canvas* onGetCanvas(Gfx::Canvas* reuse) override
216  {
217  Gfx::Canvas* canvas = _surface ? _surface->getCanvas(reuse)
218  : 0;
219  if( ! canvas )
220  return canvas;
221 
222  Gfx::RectF region = canvas->region();
223  region.move( _position.x(), _position.y() );
224  region.setSize( _view->size() );
225 
226  canvas->setRegion(region);
227  return canvas;
228  }
229 
230  virtual Gfx::Canvas* onCreateCanvas(Gfx::Canvas* reuse) override
231  {
232  assert(false && "ViewSurface does not create its own canvas");
233  return 0;
234  }
235 
236  virtual void onReleaseCanvas() override
237  {
238  // context is released by parent surface
239  }
240 
241  virtual void onSync() override
242  {
243  // sync is done by parent surface
244  }
245 
246  virtual void onFinish() override
247  {
248  // sync is done by parent surface
249  }
250 
251  private:
252  Widget* _view;
253  PaintSurface* _surface;
254  Gfx::PointF _position;
255 };
256 
257 } // namespace
258 
259 } // namespace
260 
261 #endif // include guard
Core module.
Definition: Allocator.h:33
Paint surface.
Definition: PaintSurface.h:44
Paint context.
Definition: PaintContext.h:45
Size with floating-point width and height.
Definition: Size.h:47
Common base for objects that participate in the Forms runtime.
Definition: Widget.h:73
Point with floating-point X and Y coordinates.
Definition: Point.h:47
A view that can be attached as application content.
Definition: Control.h:84
Screen of a display.
Definition: Screen.h:64
A widget that connects controls to a paint surface.
Definition: View.h:66
Rect with floating-point coordinates.
Definition: Rect.h:47