css_layout.h

Go to the documentation of this file.
00001 /*
00002 **  ClanLib SDK
00003 **  Copyright (c) 1997-2011 The ClanLib Team
00004 **
00005 **  This software is provided 'as-is', without any express or implied
00006 **  warranty.  In no event will the authors be held liable for any damages
00007 **  arising from the use of this software.
00008 **
00009 **  Permission is granted to anyone to use this software for any purpose,
00010 **  including commercial applications, and to alter it and redistribute it
00011 **  freely, subject to the following restrictions:
00012 **
00013 **  1. The origin of this software must not be misrepresented; you must not
00014 **     claim that you wrote the original software. If you use this software
00015 **     in a product, an acknowledgment in the product documentation would be
00016 **     appreciated but is not required.
00017 **  2. Altered source versions must be plainly marked as such, and must not be
00018 **     misrepresented as being the original software.
00019 **  3. This notice may not be removed or altered from any source distribution.
00020 **
00021 **  Note: Some of the libraries ClanLib may link to may have additional
00022 **  requirements or restrictions.
00023 **
00024 **  File Author(s):
00025 **
00026 **    Magnus Norddahl
00027 */
00028 
00029 #pragma once
00030 
00031 #include "api_csslayout.h"
00032 #include "../Core/Signals/callback_2.h"
00033 #include "../Core/System/uniqueptr.h"
00034 #include <memory>
00035 
00036 class CL_CSSBoxElement;
00037 class CL_GraphicContext;
00038 class CL_CSSLayoutObject;
00039 class CL_CSSLayoutElement;
00040 class CL_CSSLayoutText;
00041 class CL_CSSLayoutNode;
00042 class CL_CSSHitTestResult;
00043 class CL_CSSLayout_Impl;
00044 class CL_Size;
00045 class CL_Point;
00046 class CL_Image;
00047 class CL_Rect;
00048 
00049 class CL_API_CSSLAYOUT CL_CSSLayout
00050 {
00051 public:
00052         CL_CSSLayout();
00053 
00054         bool is_null() const;
00055 
00056         void load_xml(const CL_String &filename, const CL_String &style_sheet);
00057         void layout(CL_GraphicContext &gc, const CL_Rect &viewport);
00058         void render(CL_GraphicContext &gc) { render_impl(gc); }
00059 
00060         template<typename GUIComponent>
00061         void render(CL_GraphicContext &gc, GUIComponent *component)
00062         {
00063                 render_impl(gc, CL_UniquePtr<ClipWrapper>(new GUIComponentWrapper<GUIComponent>(component)));
00064         }
00065 
00066         CL_CSSHitTestResult hit_test(CL_GraphicContext &gc, const CL_Point &pos);
00067         void clear_selection();
00068         void set_selection(CL_CSSLayoutNode start, size_t start_text_offset, CL_CSSLayoutNode end, size_t end_text_offset);
00069 
00070         void clear();
00071         void set_root_element(CL_CSSLayoutElement element);
00072         CL_CSSLayoutElement get_root_element();
00073 
00074         void set_html_body_element(CL_CSSLayoutElement element);
00075         CL_CSSLayoutElement get_html_body_element();
00076 
00077         CL_CSSLayoutObject create_object();
00078         CL_CSSLayoutElement create_element(const CL_String &name = CL_String());
00079         CL_CSSLayoutText create_text(const CL_String &text);
00080 
00081         CL_CSSLayoutElement find_element(const CL_String &name);
00082 
00083         // CL_Image on_get_image(CL_GraphicContext &gc, const CL_String &uri);
00084         CL_Callback_2<CL_Image, CL_GraphicContext &, const CL_String &> &func_get_image();
00085 
00086         class ClipWrapper
00087         {
00088         public:
00089                 virtual ~ClipWrapper() { }
00090                 virtual void set_cliprect(CL_GraphicContext &gc, const CL_Rect &rect) = 0;
00091                 virtual void reset_cliprect(CL_GraphicContext &gc) = 0;
00092                 virtual void push_cliprect(CL_GraphicContext &gc, const CL_Rect &rect) = 0;
00093                 virtual void pop_cliprect(CL_GraphicContext &gc) = 0;
00094         };
00095 
00096 private:
00097         void render_impl(CL_GraphicContext &gc, CL_UniquePtr<ClipWrapper> wrapper = CL_UniquePtr<ClipWrapper>());
00098 
00099         template<typename GUIComponent>
00100         class GUIComponentWrapper : public ClipWrapper
00101         {
00102         public:
00103                 GUIComponentWrapper(GUIComponent *component) : component(component) { }
00104                 void set_cliprect(CL_GraphicContext &gc, const CL_Rect &rect) { component->set_cliprect(gc, rect); }
00105                 void reset_cliprect(CL_GraphicContext &gc) { component->reset_cliprect(gc); }
00106                 void push_cliprect(CL_GraphicContext &gc, const CL_Rect &rect) { component->push_cliprect(gc, rect); }
00107                 void pop_cliprect(CL_GraphicContext &gc) { component->pop_cliprect(gc); }
00108 
00109         private:
00110                 GUIComponent *component;
00111         };
00112 
00113         CL_CSSLayout(CL_SharedPtr<CL_CSSLayout_Impl> impl);
00114         CL_SharedPtr<CL_CSSLayout_Impl> impl;
00115         friend class CL_CSSLayout_Impl;
00116 };