1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
|
/*
* This source file is part of libRocket, the HTML/CSS Interface Middleware
*
* For the latest information, see http://www.librocket.com
*
* Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#include "precompiled.h"
#include "ElementInterface.h"
#include "../../../Include/Rocket/Core/Python/Utilities.h"
#include "../../../Include/Rocket/Core/Python/VectorInterface.h"
#include "../ElementHandle.h"
#include "../ElementImage.h"
#include "../ElementTextDefault.h"
#include "../../../Include/Rocket/Core/ElementUtilities.h"
#include "../../../Include/Rocket/Core/Factory.h"
#include "ElementAttributeProxy.h"
#include "ElementChildrenProxy.h"
#include "ElementDocumentWrapper.h"
#include "ElementStyleProxy.h"
#include "../../../Include/Rocket/Core/Python/ElementInstancer.h"
#include "../../../Include/Rocket/Core/Python/ElementWrapper.h"
namespace Rocket {
namespace Core {
namespace Python {
typedef std::map< Rocket::Core::String, PyObject* > ClassDefinitions;
ClassDefinitions class_definitions;
void ElementInterface::InitialisePythonInterface()
{
ElementStyleProxy::InitialisePythonInterface();
ElementChildrenProxy::InitialisePythonInterface();
ElementAttributeProxy::InitialisePythonInterface();
// Element list
Rocket::Core::Python::VectorInterface< ElementList > element_list("ElementList");
// Document focus flags
python::enum_< ElementDocument::FocusFlags >("focus")
.value("NONE", ElementDocument::NONE)
.value("FOCUS", ElementDocument::FOCUS)
.value("MODAL", ElementDocument::MODAL)
;
void (*AddEventListener)(Element* element, const char* event, Rocket::Core::Python::EventListener* listener, bool in_capture_phase) = &ElementInterface::AddEventListener;
void (*AddEventListenerDefault)(Element* element, const char* event, Rocket::Core::Python::EventListener* listener) = &ElementInterface::AddEventListener;
// Define the basic element type.
class_definitions["Element"] = python::class_< Element, ElementWrapper< Element >, boost::noncopyable >("Element", python::init< const char* >())
.def("AddEventListener", AddEventListener)
.def("AddEventListener", AddEventListenerDefault)
.def("AppendChild", &ElementInterface::AppendChild)
.def("Blur", &Element::Blur)
.def("Click", &Element::Click)
.def("DispatchEvent", &ElementInterface::DispatchEvent)
.def("Focus", &Element::Focus)
.def("GetAttribute", python::make_function(&ElementInterface::GetAttribute, python::return_value_policy< python::return_by_value >()))
.def("GetElementById", &Element::GetElementById, python::return_value_policy< python::return_by_value >())
.def("GetElementsByTagName", &ElementInterface::GetElementsByTagName)
.def("HasAttribute", &Element::HasAttribute)
.def("HasChildNodes", &Element::HasChildNodes)
.def("IsPseudoClassSet", &Element::IsPseudoClassSet)
.def("InsertBefore", &Element::InsertBefore)
.def("RemoveAttribute", &Element::RemoveAttribute)
.def("RemoveChild", &Element::RemoveChild)
.def("ReplaceChild", &Element::ReplaceChild)
.def("ScrollIntoView", &Element::ScrollIntoView)
.def("SetAttribute", &ElementInterface::SetAttribute)
.def("SetPseudoClass", &Element::SetPseudoClass)
.def("SetClass", &Element::SetClass)
.def("IsClassSet", &Element::IsClassSet)
.add_property("absolute_left", &Element::GetAbsoluteLeft)
.add_property("absolute_top", &Element::GetAbsoluteTop)
.add_property("address", python::make_function(&ElementInterface::GetAddress, python::return_value_policy< python::return_by_value >()))
.add_property("attributes", &ElementInterface::GetAttributes)
.add_property("child_nodes", &ElementInterface::GetChildren)
.add_property("class_name", python::make_function(&Element::GetClassNames, python::return_value_policy< python::return_by_value >()), &Element::SetClassNames)
.add_property("client_left", &Element::GetClientLeft)
.add_property("client_height", &Element::GetClientHeight)
.add_property("client_top", &Element::GetClientTop)
.add_property("client_width", &Element::GetClientWidth)
.add_property("first_child", python::make_function(&Element::GetFirstChild, python::return_value_policy< python::return_by_value >()))
.add_property("id", python::make_function(&Element::GetId, python::return_value_policy< python::return_by_value >()), &Element::SetId)
.add_property("inner_rml", &ElementInterface::GetInnerRML, &Element::SetInnerRML)
.add_property("last_child", python::make_function(&Element::GetLastChild, python::return_value_policy< python::return_by_value >()))
.add_property("next_sibling", python::make_function(&Element::GetNextSibling, python::return_value_policy< python::return_by_value >()))
.add_property("offset_height", &Element::GetOffsetHeight)
.add_property("offset_left", &Element::GetOffsetLeft)
.add_property("offset_parent", python::make_function(&Element::GetOffsetParent, python::return_value_policy< python::return_by_value >()))
.add_property("offset_top", &Element::GetOffsetTop)
.add_property("offset_width", &Element::GetOffsetWidth)
.add_property("owner_document", python::make_function(&Element::GetOwnerDocument, python::return_value_policy< python::return_by_value >()))
.add_property("parent_node", python::make_function(&Element::GetParentNode, python::return_value_policy< python::return_by_value >()))
.add_property("previous_sibling", python::make_function(&Element::GetPreviousSibling, python::return_value_policy< python::return_by_value >()))
.add_property("scroll_height", &Element::GetScrollHeight)
.add_property("scroll_left", &Element::GetScrollLeft, &Element::SetScrollLeft)
.add_property("scroll_top", &Element::GetScrollTop, &Element::SetScrollTop)
.add_property("scroll_width", &Element::GetScrollWidth)
.add_property("style", &ElementInterface::GetStyle)
.add_property("tag_name", python::make_function(&Element::GetTagName, python::return_value_policy< python::return_by_value >()))
.ptr();
// Define the document type.
class_definitions["Document"] = python::class_< ElementDocument, ElementDocumentWrapper, python::bases< Element >, boost::noncopyable >("Document", python::init< const char* >())
.def("PullToFront", &ElementDocument::PullToFront)
.def("PushToBack", &ElementDocument::PushToBack)
.def("Show", &ElementInterface::Show)
.def("Show", &ElementDocument::Show)
.def("Hide", &ElementDocument::Hide)
.def("Close", &ElementDocument::Close)
.def("CreateElement", &ElementInterface::CreateElement)
.def("CreateTextNode", &ElementInterface::CreateTextNode)
.add_property("title", python::make_function(&ElementDocument::GetTitle, python::return_value_policy< python::return_by_value >()), &ElementDocument::SetTitle)
.add_property("context", python::make_function(&ElementDocument::GetContext, python::return_value_policy< python::return_by_value >()))
.ptr();
// The ElementText type.
python::class_< ElementText, ElementWrapper< ElementText >, boost::noncopyable, python::bases< Element > >("IElementText", python::no_init);
class_definitions["Text"] = python::class_< ElementTextDefault, ElementWrapper< ElementTextDefault >, boost::noncopyable, python::bases< ElementText > >("Text", python::init< const char* >())
.add_property("text", &ElementInterface::GetText, &ElementInterface::SetText)
.ptr();
// The ElementImage type.
class_definitions["Image"] = python::class_< ElementImage, ElementWrapper< ElementImage >, boost::noncopyable, python::bases< Element > >("Image", python::init< const char* >())
.ptr();
// The ElementHandle type.
class_definitions["Handle"] = python::class_< ElementHandle, ElementWrapper< ElementHandle >, boost::noncopyable, python::bases< Element > >("Handle", python::init< const char* >())
.ptr();
}
void ElementInterface::InitialiseRocketInterface()
{
// Redefine the generic instancer
Factory::RegisterElementInstancer("*", new ElementInstancer((*class_definitions.find("Element")).second))->RemoveReference();
Factory::RegisterElementInstancer("#text", new ElementInstancer((*class_definitions.find("Text")).second))->RemoveReference();
Factory::RegisterElementInstancer("body", new ElementInstancer((*class_definitions.find("Document")).second))->RemoveReference();
Factory::RegisterElementInstancer("handle", new ElementInstancer((*class_definitions.find("Handle")).second))->RemoveReference();
Factory::RegisterElementInstancer("img", new ElementInstancer((*class_definitions.find("Image")).second))->RemoveReference();
}
// Get the element's address.
Rocket::Core::String ElementInterface::GetAddress(Element* element)
{
return element->GetAddress(false);
}
ElementStyleProxy ElementInterface::GetStyle(Element* element)
{
return ElementStyleProxy(element);
}
ElementChildrenProxy ElementInterface::GetChildren(Element* element)
{
return ElementChildrenProxy(element);
}
ElementAttributeProxy ElementInterface::GetAttributes(Element* element)
{
return ElementAttributeProxy(element);
}
void ElementInterface::AddEventListener(Element* element, const char* event, EventListener* listener, bool in_capture_phase)
{
element->AddEventListener(event, listener, in_capture_phase);
}
void ElementInterface::AddEventListener(Element* element, const char* event, EventListener* listener)
{
element->AddEventListener(event, listener);
}
// Override for AppendChild without the non-DOM boolean.
void ElementInterface::AppendChild(Element* element, Element* child)
{
element->AppendChild(child);
}
void ElementInterface::DispatchEvent(Element* element, const char* event, const python::dict& parameters, bool interruptible)
{
Rocket::Core::Dictionary ROCKET_parameters;
PyObject* keys = PyDict_Keys(parameters.ptr());
int num_keys = PyList_Size(keys);
for (int i = 0; i < num_keys; ++i)
{
PyObject* py_key = PyList_GetItem(keys, i);
if (!PyUnicode_Check(py_key))
{
Py_DECREF(keys);
PyErr_SetString(PyExc_KeyError, "Only string keys supported.");
python::throw_error_already_set();
}
Rocket::Core::Variant value;
if (!Rocket::Core::Python::Utilities::ConvertToVariant(value, PyDict_GetItem(parameters.ptr(), py_key)))
{
Py_DECREF(keys);
PyErr_SetString(PyExc_ValueError, "Unable to convert parameter value.");
python::throw_error_already_set();
}
const char* key = PyUnicode_AsUTF8(py_key);
ROCKET_parameters.Set(key, value);
}
element->DispatchEvent(event, ROCKET_parameters, interruptible);
}
Rocket::Core::String ElementInterface::GetAttribute(Element* element, const char* name)
{
return element->GetAttribute< Rocket::Core::String >(name, "");
}
// Returns the list of elements.
ElementList ElementInterface::GetElementsByTagName(Element* element, const char* tag)
{
ElementList elements;
element->GetElementsByTagName(elements, tag);
return elements;
}
void ElementInterface::SetAttribute(Element* element, const char* name, const char* value)
{
element->SetAttribute(name, value);
}
Rocket::Core::String ElementInterface::GetInnerRML(Element* element)
{
Rocket::Core::String rml;
element->GetInnerRML(rml);
return rml;
}
Rocket::Core::String ElementInterface::GetText(ElementText* element)
{
Rocket::Core::String text;
element->GetText().ToUTF8(text);
return text;
}
void ElementInterface::SetText(ElementText* element, const char* text)
{
element->SetText(text);
}
void ElementInterface::Show(ElementDocument* document)
{
document->Show();
}
// Creates a correctly reference-counted element.
python::object ElementInterface::CreateElement(ElementDocument* document, const char* tag)
{
Element* new_element = document->CreateElement(tag);
if (new_element == NULL)
return python::object();
python::object py_element = Rocket::Core::Python::Utilities::MakeObject(new_element);
new_element->RemoveReference();
return py_element;
}
// Creates a correctly reference-counted text node.
python::object ElementInterface::CreateTextNode(ElementDocument* document, const char* text)
{
Element* new_element = document->CreateTextNode(text);
if (new_element == NULL)
return python::object();
python::object py_element = Rocket::Core::Python::Utilities::MakeObject(new_element);
new_element->RemoveReference();
return py_element;
}
}
}
}
|