view_tree.h
1/*
2** ClanLib SDK
3** Copyright (c) 1997-2020 The ClanLib Team
4**
5** This software is provided 'as-is', without any express or implied
6** warranty. In no event will the authors be held liable for any damages
7** arising from the use of this software.
8**
9** Permission is granted to anyone to use this software for any purpose,
10** including commercial applications, and to alter it and redistribute it
11** freely, subject to the following restrictions:
12**
13** 1. The origin of this software must not be misrepresented; you must not
14** claim that you wrote the original software. If you use this software
15** in a product, an acknowledgment in the product documentation would be
16** appreciated but is not required.
17** 2. Altered source versions must be plainly marked as such, and must not be
18** misrepresented as being the original software.
19** 3. This notice may not be removed or altered from any source distribution.
20**
21** Note: Some of the libraries ClanLib may link to may have additional
22** requirements or restrictions.
23**
24** File Author(s):
25**
26** Magnus Norddahl
27*/
28
29#pragma once
30
31#include "../View/view.h"
32#include "../Events/activation_change_event.h"
33
34namespace clan
35{
36 class DisplayWindow;
37 class Canvas;
38 class ViewTreeImpl;
39
42 {
43 public:
45 virtual ~ViewTree();
46
48 View *focus_view() const;
49
54
56 virtual Canvas canvas() const = 0;
57
59 const std::shared_ptr<View> &root_view() const;
60
62 void set_root_view(std::shared_ptr<View> root_view);
63
65 void add_child(const std::shared_ptr<View> &view)
66 {
67 root_view()->add_child(view);
68 }
69
70 template<typename T, typename... Types>
71 std::shared_ptr<T> add_child(Types &&... args)
72 {
73 auto child = std::make_shared<T>(std::forward<Types>(args)...);
74 add_child(child);
75 return child;
76 }
77
78 std::shared_ptr<View> add_child()
79 {
80 return add_child<View>();
81 }
82
83 protected:
85 void set_focus_view(View *view);
86
88 void render(Canvas &canvas, const Rectf &margin_box);
89
92
94 virtual void set_needs_render() = 0;
95
97 virtual Pointf client_to_screen_pos(const Pointf &pos) = 0;
98
100 virtual Pointf screen_to_client_pos(const Pointf &pos) = 0;
101
102 private:
103 ViewTree(const ViewTree &) = delete;
104 ViewTree &operator=(const ViewTree &) = delete;
105
106 std::unique_ptr<ViewTreeImpl> impl;
107
108 friend class View;
109 friend class ViewImpl;
110 friend class ViewController;
111 friend class PositionedLayout;
112 };
113}
2D Graphics Canvas
Definition canvas.h:72
Top-level window class.
Definition display_window.h:101
2D (x,y) point structure - Float
Definition point.h:72
2D (left,top,right,bottom) rectangle structure - Float
Definition rect.h:460
friend class View
Definition view_tree.h:108
void dispatch_activation_change(ActivationChangeType type)
Dispatch activation change event to all views.
friend class PositionedLayout
Definition view_tree.h:111
virtual void set_needs_render()=0
Signals that the root view needs to be rendered again.
virtual DisplayWindow display_window()=0
virtual Canvas canvas() const =0
Gets the current canvas used to render.
std::shared_ptr< T > add_child(Types &&... args)
Definition view_tree.h:71
friend class ViewImpl
Definition view_tree.h:109
std::shared_ptr< View > add_child()
Definition view_tree.h:78
friend class ViewController
Definition view_tree.h:110
void add_child(const std::shared_ptr< View > &view)
Add a child view.
Definition view_tree.h:65
void set_root_view(std::shared_ptr< View > root_view)
Sets a new root view controller for the view tree.
virtual ~ViewTree()
const std::shared_ptr< View > & root_view() const
Retrieves the root of the view tree.
virtual Pointf screen_to_client_pos(const Pointf &pos)=0
Map from screen to client coordinates.
virtual Pointf client_to_screen_pos(const Pointf &pos)=0
Map from client to screen coordinates.
View * focus_view() const
The view receiving keyboard events or nullptr if no view has the focus.
void set_focus_view(View *view)
Set or clears the focus.
void render(Canvas &canvas, const Rectf &margin_box)
Renders view into the specified canvas.
Definition clanapp.h:36
ActivationChangeType
Window activation change.
Definition activation_change_event.h:37