view_tree.h
1 /*
2 ** UICore
3 ** Copyright (c) 1997-2015 The UICore 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 UICore 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 
34 namespace uicore
35 {
36  class DisplayWindow;
37  typedef std::shared_ptr<DisplayWindow> DisplayWindowPtr;
38  class Canvas;
39  class ViewTreeImpl;
40 
42  class ViewTree
43  {
44  public:
45  ViewTree();
46  virtual ~ViewTree();
47 
49  View *focus_view() const;
50 
54  virtual DisplayWindowPtr display_window() = 0;
55 
57  virtual CanvasPtr canvas() const = 0;
58 
60  const std::shared_ptr<View> &root_view() const;
61 
63  void set_root_view(std::shared_ptr<View> root_view);
64 
66  void add_child(const std::shared_ptr<View> &view)
67  {
68  root_view()->add_child(view);
69  }
70 
71  template<typename T, typename... Types>
72  std::shared_ptr<T> add_child(Types &&... args)
73  {
74  auto child = std::make_shared<T>(std::forward<Types>(args)...);
75  add_child(child);
76  return child;
77  }
78 
79  std::shared_ptr<View> add_child()
80  {
81  return add_child<View>();
82  }
83 
84  protected:
86  void set_focus_view(View *view);
87 
89  void render(const CanvasPtr &canvas, const Rectf &margin_box);
90 
93 
95  virtual void set_needs_render() = 0;
96 
98  virtual Pointf client_to_screen_pos(const Pointf &pos) = 0;
99 
101  virtual Pointf screen_to_client_pos(const Pointf &pos) = 0;
102 
103  private:
104  ViewTree(const ViewTree &) = delete;
105  ViewTree &operator=(const ViewTree &) = delete;
106 
107  std::unique_ptr<ViewTreeImpl> impl;
108 
109  friend class View;
110  friend class ViewImpl;
111  friend class ViewController;
112  friend class PositionedLayout;
113  };
114 }
virtual CanvasPtr canvas() const =0
Gets the current canvas used to render.
void dispatch_activation_change(ActivationChangeType type)
Dispatch activation change event to all views.
2D (x,y) point structure - Float
Definition: point.h:68
friend class ViewController
Definition: view_tree.h:111
Base class for managing a tree of views.
Definition: view_tree.h:42
friend class PositionedLayout
Definition: view_tree.h:112
std::shared_ptr< DisplayWindow > DisplayWindowPtr
Definition: canvas.h:41
std::shared_ptr< T > add_child(Types &&...args)
Definition: view_tree.h:72
void add_child(const std::shared_ptr< View > &view)
Add a child view.
Definition: view_tree.h:66
void set_focus_view(View *view)
Set or clears the focus.
virtual Pointf screen_to_client_pos(const Pointf &pos)=0
Map from screen to client coordinates.
2D (left,top,right,bottom) rectangle structure - Float
Definition: rect.h:505
virtual ~ViewTree()
ActivationChangeType
Window activation change.
Definition: activation_change_event.h:36
std::shared_ptr< View > add_child()
Definition: view_tree.h:79
virtual void set_needs_render()=0
Signals that the root view needs to be rendered again.
const std::shared_ptr< View > & root_view() const
Retrieves the root of the view tree.
View for an area of the user interface.
Definition: view.h:68
View * focus_view() const
The view receiving keyboard events or nullptr if no view has the focus.
void set_root_view(std::shared_ptr< View > root_view)
Sets a new root view controller for the view tree.
std::shared_ptr< Canvas > CanvasPtr
Definition: canvas.h:126
friend class ViewImpl
Definition: view_tree.h:110
void render(const CanvasPtr &canvas, const Rectf &margin_box)
Renders view into the specified canvas.
virtual DisplayWindowPtr display_window()=0
Definition: Application/application.h:35
virtual Pointf client_to_screen_pos(const Pointf &pos)=0
Map from client to screen coordinates.