EventInstancer.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 "../../../Include/Rocket/Core/Event.h"
  29. #include "../../../Include/Rocket/Core/Python/Utilities.h"
  30. #include "EventWrapper.h"
  31. #include "EventInstancer.h"
  32. namespace Rocket {
  33. namespace Core {
  34. namespace Python {
  35. EventInstancer::EventInstancer(PyObject* _instancer)
  36. {
  37. instancer = _instancer;
  38. Py_INCREF( _instancer );
  39. }
  40. EventInstancer::~EventInstancer()
  41. {
  42. Py_DECREF( instancer );
  43. }
  44. Event* EventInstancer::InstanceEvent(Element* target, const Rocket::Core::String& name, const Rocket::Core::Dictionary& parameters, bool interruptable)
  45. {
  46. EventWrapper* event = NULL;
  47. try
  48. {
  49. // Convert parameters to a python representation
  50. python::object params(parameters);
  51. // Increase reference count on required args
  52. Py_INCREF(params.ptr());
  53. // Put the arguments into a tuple
  54. PyObject* args = PyTuple_New(3);
  55. PyTuple_SetItem(args, 0, PyString_FromString(name.CString()));
  56. PyTuple_SetItem(args, 1, params.ptr());
  57. PyTuple_SetItem(args, 2, PyBool_FromLong(interruptable));
  58. // Instance
  59. PyObject* instance = PyObject_CallObject(instancer, args);
  60. Py_DECREF(args);
  61. // Rebind the target element
  62. if (instance)
  63. {
  64. event = python::extract<EventWrapper*>(Rocket::Core::Python::Utilities::MakeObject(instance));
  65. if (event)
  66. {
  67. event->SetTargetElement(target);
  68. }
  69. }
  70. }
  71. catch (python::error_already_set&)
  72. {
  73. event = NULL;
  74. }
  75. if (!event)
  76. {
  77. Rocket::Core::Python::Utilities::PrintError(true);
  78. }
  79. return event;
  80. }
  81. // Releases an event instanced by this instancer.
  82. void EventInstancer::ReleaseEvent(Event* event)
  83. {
  84. EventWrapper* event_wrapper = dynamic_cast< EventWrapper* >(event);
  85. if (event_wrapper != NULL)
  86. {
  87. // Little bit of footwork so that the destructors security check passes
  88. PyObject* _self = event_wrapper->self;
  89. event_wrapper->self = NULL;
  90. Py_DECREF( _self );
  91. }
  92. }
  93. void EventInstancer::Release()
  94. {
  95. delete this;
  96. }
  97. }
  98. }
  99. }