ContextInterface.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "precompiled.h"
  28. #include "ContextInterface.h"
  29. #include "../../../Include/Rocket/Core/Python/Utilities.h"
  30. #include "../../../Include/Rocket/Core/Python/Wrapper.h"
  31. #include "../../../Include/Rocket/Core/Context.h"
  32. #include "../../../Include/Rocket/Core/Factory.h"
  33. #include "EventListener.h"
  34. #include "ContextInstancer.h"
  35. namespace Rocket {
  36. namespace Core {
  37. namespace Python {
  38. static PyObject* py_context = NULL;
  39. // Initialises the Python interface.
  40. bool ContextInterface::InitialisePythonInterface()
  41. {
  42. ContextDocumentProxy::InitialisePythonInterface();
  43. py_context = python::class_< Context, Rocket::Core::Python::Wrapper< Context, const Rocket::Core::String& >, boost::noncopyable>("Context", python::init< const Rocket::Core::String& >())
  44. .def("AddEventListener", &ContextInterface::AddEventListener)
  45. .def("AddMouseCursor", &Context::AddMouseCursor, python::return_value_policy< python::return_by_value >())
  46. .def("CreateDocument", &ContextInterface::CreateDocument)
  47. .def("LoadDocument", &ContextInterface::LoadDocument)
  48. .def("LoadDocumentFromMemory", &ContextInterface::LoadDocumentFromMemory)
  49. .def("LoadMouseCursor", &ContextInterface::LoadMouseCursor)
  50. .def("Render", &Context::Render)
  51. .def("ShowMouseCursor", &Context::ShowMouseCursor)
  52. .def("UnloadAllDocuments", &Context::UnloadAllDocuments)
  53. .def("UnloadAllMouseCursors", &Context::UnloadAllMouseCursors)
  54. .def("UnloadDocument", &Context::UnloadDocument)
  55. .def("UnloadMouseCursor", &Context::UnloadMouseCursor)
  56. .def("Update", &Context::Update)
  57. .add_property("dimensions", python::make_function(&Context::GetDimensions, python::return_value_policy< python::return_by_value >()), &Context::SetDimensions)
  58. .add_property("documents", &ContextInterface::GetDocuments)
  59. .add_property("focus_element", python::make_function(&Context::GetFocusElement, python::return_value_policy< python::return_by_value >()))
  60. .add_property("hover_element", python::make_function(&Context::GetHoverElement, python::return_value_policy< python::return_by_value >()))
  61. .add_property("root_element", python::make_function(&Context::GetRootElement, python::return_value_policy< python::return_by_value >()))
  62. .add_property("name", python::make_function(&Context::GetName, python::return_value_policy< python::return_by_value >()))
  63. .ptr();
  64. return true;
  65. }
  66. // Initialise the Rocket element interface.
  67. void ContextInterface::InitialiseRocketInterface()
  68. {
  69. Factory::RegisterContextInstancer(new ContextInstancer(py_context))->RemoveReference();
  70. }
  71. // The "AddEventListener" function bound into Python context objects instead of the C++ function.
  72. void ContextInterface::AddEventListener(Context* self, const char* event, const char* script, bool in_capture_phase)
  73. {
  74. self->AddEventListener(event, new EventListener(script, self->GetRootElement()), in_capture_phase);
  75. }
  76. // The "CreateDocument" function bound into Python context objects instead of the C++ function.
  77. python::object ContextInterface::CreateDocument(Context* self, const char* tag)
  78. {
  79. Rocket::Core::ElementDocument* document = self->CreateDocument(tag);
  80. if (document == NULL)
  81. return python::object();
  82. // Remove the C++ caller reference and add a Python one to replace it.
  83. python::object py_document = Rocket::Core::Python::Utilities::MakeObject(document);
  84. document->RemoveReference();
  85. return py_document;
  86. }
  87. // The "LoadDocument" function bound into Python context objects instead of the C++ function.
  88. python::object ContextInterface::LoadDocument(Context* self, const char* document_path)
  89. {
  90. Rocket::Core::ElementDocument* document = self->LoadDocument(document_path);
  91. if (document == NULL)
  92. return python::object();
  93. // Remove the C++ caller reference and return the python::object
  94. python::object py_document = Rocket::Core::Python::Utilities::MakeObject(document);
  95. document->RemoveReference();
  96. return py_document;
  97. }
  98. // The "LoadDocument" function bound into Python context objects instead of the C++ function.
  99. python::object ContextInterface::LoadDocumentFromMemory(Context* self, const char* stream)
  100. {
  101. Rocket::Core::ElementDocument* document = self->LoadDocumentFromMemory(stream);
  102. if (document == NULL)
  103. return python::object();
  104. // Remove the C++ caller reference and return the python::object
  105. python::object py_document = Rocket::Core::Python::Utilities::MakeObject(document);
  106. document->RemoveReference();
  107. return py_document;
  108. }
  109. // The "LoadMouseCursor" function bound into Python context objects instead of the C++ function.
  110. python::object ContextInterface::LoadMouseCursor(Context* self, const char* document_path)
  111. {
  112. Rocket::Core::ElementDocument* document = self->LoadMouseCursor(document_path);
  113. if (document == NULL)
  114. return python::object();
  115. // Remove the C++ caller reference and add a Python one to replace it.
  116. python::object py_document = Rocket::Core::Python::Utilities::MakeObject(document);
  117. document->RemoveReference();
  118. return py_document;
  119. }
  120. // Returns the document proxy object for the 'document' property.
  121. ContextDocumentProxy ContextInterface::GetDocuments(Context* self)
  122. {
  123. return ContextDocumentProxy(self);
  124. }
  125. }
  126. }
  127. }