css_layout.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2013 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 "../api_csslayout.h"
32 #include "../../Core/Signals/callback_2.h"
33 #include <memory>
34 
35 namespace clan
36 {
39 
40 class CSSBoxElement;
41 class CSSLayoutObject;
42 class CSSLayoutElement;
43 class CSSLayoutText;
44 class CSSLayoutNode;
45 class CSSHitTestResult;
46 class CSSDocument;
47 class CSSLayout_Impl;
48 class GraphicContext;
49 class Size;
50 class Point;
51 class Image;
52 class Rect;
53 class Canvas;
54 
55 class CL_API_CSSLAYOUT CSSLayout
56 {
57 public:
58  CSSLayout();
59 
60  bool is_null() const;
61 
62  void set_dpi(float new_dpi);
63 
64  void set_css_document(const CSSDocument &doc);
65 
66  void layout(Canvas &canvas, const Rect &viewport);
67  void render(Canvas &canvas) { render_impl(canvas); }
68 
69  template<typename GUIElement>
70  void render(Canvas &canvas, GUIElement *component)
71  {
72  render_impl(canvas, std::unique_ptr<ClipWrapper>(new GUIElementWrapper<GUIElement>(component)));
73  }
74 
75  CSSHitTestResult hit_test(Canvas &canvas, const Point &pos);
76 
77  void set_document_element(CSSLayoutElement element);
78  CSSLayoutElement get_document_element();
79 
80  CSSLayoutObject create_object();
81  CSSLayoutElement create_element(const std::string &name = std::string());
82  CSSLayoutText create_text(const std::string &text);
83 
84  CSSLayoutElement find_element(const std::string &name);
85 
86  // Image on_get_image(Canvas &canvas, const std::string &uri);
88 
90  {
91  public:
92  virtual ~ClipWrapper() { }
93  virtual void set_cliprect(Canvas &canvas, const Rect &rect) = 0;
94  virtual void reset_cliprect(Canvas &canvas) = 0;
95  virtual void push_cliprect(Canvas &canvas, const Rect &rect) = 0;
96  virtual void pop_cliprect(Canvas &canvas) = 0;
97  };
98 
99 private:
100  void render_impl(Canvas &canvas, std::unique_ptr<ClipWrapper> wrapper = std::unique_ptr<ClipWrapper>());
101 
102  template<typename GUIElement>
103  class GUIElementWrapper : public ClipWrapper
104  {
105  public:
106  GUIElementWrapper(GUIElement *component) : component(component) { }
107  void set_cliprect(Canvas &canvas, const Rect &rect) { component->set_cliprect(canvas, rect); }
108  void reset_cliprect(Canvas &canvas) { component->reset_cliprect(canvas); }
109  void push_cliprect(Canvas &canvas, const Rect &rect) { component->push_cliprect(canvas, rect); }
110  void pop_cliprect(Canvas &canvas) { component->pop_cliprect(canvas); }
111 
112  private:
113  GUIElement *component;
114  };
115 
116  CSSLayout(std::shared_ptr<CSSLayout_Impl> impl);
117  std::shared_ptr<CSSLayout_Impl> impl;
118  friend class CSSLayout_Impl;
119 };
120 
122 }
void render(Canvas &canvas, GUIElement *component)
Definition: css_layout.h:70
Definition: css_document.h:55
2D Graphics Canvas
Definition: canvas.h:70
virtual ~ClipWrapper()
Definition: css_layout.h:92
2D (left,top,right,bottom) rectangle structure - Integer
Definition: rect.h:453
Definition: css_hit_test_result.h:9
Definition: css_layout.h:55
Definition: css_layout_text.h:40
2D (x,y) point structure - Integer
Definition: point.h:63
Callback_2.
Definition: callback_2.h:156
Definition: css_layout_object.h:40
Definition: css_layout.h:89
void render(Canvas &canvas)
Definition: css_layout.h:67
Definition: css_layout_element.h:42