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
47namespace Pt {
48
49namespace Forms {
50
51class Control;
52class PaintContext;
53
85class PT_FORMS_API View : public Widget
86{
87 friend class Control;
88
89 typedef Widget Base;
90
91 public:
108
109 protected:
113
114 public:
117 virtual ~View();
118
125 Gfx::PointF toControl(const Control& control,
126 const Gfx::PointF& pos) const;
127
134 Gfx::PointF fromControl(const Control& control,
135 const Gfx::PointF& pos) const;
136
144
147 const PaintSurface& surface() const;
148
157 const Gfx::PointF& pos = Gfx::PointF() );
158
159 protected:
166 const Gfx::PointF& pos);
167
173 virtual void onPaint(PaintContext& context,
174 const Gfx::RectF& rect);
175
176 protected:
182 virtual void onAttach(Control& control);
183
189 virtual void onDetach(Control& control);
190
197 virtual void onInit(Control& control);
198
205 virtual void onRelease(Control& control);
206
209 virtual Gfx::PointF onToControl(const Control& control,
210 const Gfx::PointF& pos) const;
211
214 virtual Gfx::PointF onFromControl(const Control& control,
215 const Gfx::PointF& pos) const;
216
217 protected:
223 virtual void onRepaintRequest(Control& control, const Gfx::RectF& rect);
224
229 virtual void onRelayoutRequest(Control& control);
230
235 virtual void onEnableRequest(Control& control, bool isEnable);
236
241 virtual void onActivateRequest(Control& control, bool active);
242
247 virtual void onShowRequest(Control& control, bool isShown);
248
254 virtual void onMoveRequest(Control& control, const Gfx::PointF& pos);
255
261 virtual void onResizeRequest(Control& control, const Gfx::SizeF& size);
262
267 virtual void onRaiseRequest(Control& control);
268
269 //
270 // Widget
271 //
272 protected:
277 virtual void onConnect(Screen& screen);
278
283 virtual void onDisconnect();
284
285
292 virtual void onPaintEvent(const PaintEvent& ev) override;
293
298 virtual void onMoveEvent(const MoveEvent& ev) override;
299
305 virtual void onResizeEvent(const ResizeEvent& ev) override;
306
307 private:
308 class ViewSurface* _surface;
309};
310
313class ViewSurface : public PaintSurface
314{
315 public:
316 explicit ViewSurface(View& view)
317 : _view(&view)
318 , _surface(0)
319 {
320 }
321
322 explicit ViewSurface(Widget& w)
323 : _view(&w)
324 , _surface(0)
325 {
326 }
327
328 PaintSurface* surface()
329 {
330 return _surface;
331 }
332
333 const Gfx::PointF& position() const
334 {
335 return _position;
336 }
337
338 void setSurface(PaintSurface* surface,
339 const Gfx::PointF& pos)
340 {
341 _surface = surface;
342 _position = pos;
343 }
344
345 protected:
346 virtual void onDrawPixmap(Gfx::Canvas& canvas,
347 const Gfx::PointF& to,
348 const Pixmap& pm,
349 const Gfx::RectF* rect) override;
350
351 protected:
352 virtual const Gfx::ImageFormat& onGetFormat() const override
353 {
354 if (_surface )
355 return _surface->format();
356
357 return Gfx::ImageFormat::argb32();
358 }
359
360 const Gfx::SizeF& onGetSize() const
361 {
362 return _view->size();
363 }
364
365 virtual const Gfx::Scaling& onGetScaling() const override
366 {
367 return _view->scaling();
368 }
369
370 virtual Gfx::Canvas* onGetCanvas(Gfx::Canvas* reuse) override
371 {
372 Gfx::Canvas* canvas = _surface ? _surface->getCanvas(reuse)
373 : 0;
374 if( ! canvas )
375 return canvas;
376
377 Gfx::RectF region = canvas->region();
378 region.move( _position.x(), _position.y() );
379 region.setSize( _view->size() );
380
381 canvas->setRegion(region);
382 return canvas;
383 }
384
385 virtual Gfx::Canvas* onCreateCanvas(Gfx::Canvas* reuse) override
386 {
387 assert(false && "ViewSurface does not create its own canvas");
388 return 0;
389 }
390
391 virtual void onReleaseCanvas() override
392 {
393 // context is released by parent surface
394 }
395
396 virtual void onSync() override
397 {
398 // sync is done by parent surface
399 }
400
401 virtual void onFinish() override
402 {
403 // sync is done by parent surface
404 }
405
406 private:
407 Widget* _view;
408 PaintSurface* _surface;
409 Gfx::PointF _position;
410};
411
412} // namespace
413
414} // namespace
415
416#endif // include guard
Reusable view used as application content.
Definition Control.h:107
Reports that a widget moved.
Definition MoveEvent.h:50
Active painting session on a Forms paint surface.
Definition PaintContext.h:51
Reports a dirty rectangle that a widget must paint.
Definition PaintEvent.h:48
Forms render target for painting.
Definition PaintSurface.h:50
PaintSurface()
Constructs an unbound paint surface.
Reports that a widget was resized.
Definition ResizeEvent.h:50
Display root for top-level windows.
Definition Screen.h:83
Widget that hosts controls on a paint surface.
Definition View.h:86
virtual void onDisconnect()
Disconnects the view from its screen.
Gfx::PointF toControl(const Control &control, const Gfx::PointF &pos) const
Converts pos from this view to control coordinates.
virtual Gfx::PointF onToControl(const Control &control, const Gfx::PointF &pos) const
Converts pos from this view to control coordinates.
Gfx::PointF fromControl(const Control &control, const Gfx::PointF &pos) const
Converts pos from control to this view coordinates.
virtual ~View()
Destroys the view and its surface adapter.
virtual void onMoveEvent(const MoveEvent &ev) override
Repaints the union of the old and new view bounds.
virtual void onPaint(PaintContext &context, const Gfx::RectF &rect)
Paints this view into context for the local rect.
virtual void onShowRequest(Control &control, bool isShown)
Receives a request to show or hide control.
virtual void onRelayoutRequest(Control &control)
Receives a request to recalculate control's layout.
virtual void onEnableRequest(Control &control, bool isEnable)
Receives a request to enable or disable control.
virtual Gfx::PointF onFromControl(const Control &control, const Gfx::PointF &pos) const
Converts pos from control to this view coordinates.
virtual void onConnect(Screen &screen)
Connects the view to screen.
virtual void onRepaintRequest(Control &control, const Gfx::RectF &rect)
Receives a repaint request for rect in control coordinates.
virtual void onPaintEvent(const PaintEvent &ev) override
Processes a paint event and invokes onPaint().
virtual void onDetach(Control &control)
Notifies the view that control was detached.
virtual void onMoveRequest(Control &control, const Gfx::PointF &pos)
Receives a request to move control to pos.
virtual void onResizeEvent(const ResizeEvent &ev) override
Repaints the union of the old and new view bounds.
PaintSurface & surface()
Returns this view's local paint-surface adapter.
virtual void onAttach(Control &control)
Notifies the view that control was attached.
void setSurface(PaintSurface *surface, const Gfx::PointF &pos=Gfx::PointF())
Assigns the surface on which this view displays its controls.
virtual void onSetSurface(PaintSurface *surface, const Gfx::PointF &pos)
Notifies the view after its surface assignment changes.
virtual void onActivateRequest(Control &control, bool active)
Receives a request to activate or deactivate control.
View()
Creates a view for a derived content host.
FocusPolicy
Defines whether a control can receive or retain focus.
Definition View.h:95
@ NoFocus
The control cannot receive focus.
Definition View.h:98
@ KeepFocus
The control retains focus during focus navigation.
Definition View.h:106
@ AcceptFocus
The control participates in normal focus navigation.
Definition View.h:102
const PaintSurface & surface() const
Returns this view's local paint-surface adapter.
virtual void onResizeRequest(Control &control, const Gfx::SizeF &size)
Receives a request to resize control to size.
virtual void onRaiseRequest(Control &control)
Receives a request to raise control in its host.
virtual void onInit(Control &control)
Initializes control after it has been attached.
virtual void onRelease(Control &control)
Releases runtime resources associated with control.
Widget()
Creates and registers a widget with the Forms application.
const Gfx::SizeF & size() const
Returns the size in local logical coordinates.
Screen * screen()
Returns the connected screen, or 0 when disconnected.
Graphical user interfaces.
Definition ActivateEvent.h:40
Core module.
Definition Allocator.h:33