LuaEventListener.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019-2023 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "LuaEventListener.h"
  29. #include <RmlUi/Core/Element.h>
  30. #include <RmlUi/Lua/Interpreter.h>
  31. #include <RmlUi/Lua/LuaType.h>
  32. #include <RmlUi/Lua/Utilities.h>
  33. namespace Rml {
  34. namespace Lua {
  35. typedef ElementDocument Document;
  36. LuaEventListener::LuaEventListener(const String& code, Element* element) : EventListener()
  37. {
  38. // compose function
  39. String function = "return function (event,element,document) ";
  40. function.append(code);
  41. function.append(" end");
  42. // make sure there is an area to save the function
  43. lua_State* L = Interpreter::GetLuaState();
  44. int top = lua_gettop(L);
  45. lua_getglobal(L, "EVENTLISTENERFUNCTIONS");
  46. if (lua_isnoneornil(L, -1))
  47. {
  48. lua_newtable(L);
  49. lua_setglobal(L, "EVENTLISTENERFUNCTIONS");
  50. lua_pop(L, 1); // pop the unsucessful getglobal
  51. lua_getglobal(L, "EVENTLISTENERFUNCTIONS");
  52. }
  53. int tbl = lua_gettop(L);
  54. // compile,execute,and save the function
  55. if (!Interpreter::LoadString(function, code) || !Interpreter::ExecuteCall(0, 1))
  56. {
  57. return;
  58. }
  59. luaFuncRef = luaL_ref(L, tbl); // creates a reference to the item at the top of the stack in to the table we just created
  60. lua_pop(L, 1); // pop the EVENTLISTENERFUNCTIONS table
  61. attached = element;
  62. if (element)
  63. owner_document = element->GetOwnerDocument();
  64. else
  65. owner_document = nullptr;
  66. strFunc = function;
  67. lua_settop(L, top);
  68. }
  69. // if it is passed in a Lua function
  70. LuaEventListener::LuaEventListener(lua_State* L, int narg, Element* element)
  71. {
  72. int top = lua_gettop(L);
  73. lua_getglobal(L, "EVENTLISTENERFUNCTIONS");
  74. if (lua_isnoneornil(L, -1))
  75. {
  76. lua_newtable(L);
  77. lua_setglobal(L, "EVENTLISTENERFUNCTIONS");
  78. lua_pop(L, 1); // pop the unsucessful getglobal
  79. lua_getglobal(L, "EVENTLISTENERFUNCTIONS");
  80. }
  81. lua_pushvalue(L, narg);
  82. luaFuncRef = luaL_ref(L, -2); // put the funtion as a ref in to that table
  83. lua_pop(L, 1); // pop the EVENTLISTENERFUNCTIONS table
  84. attached = element;
  85. if (element)
  86. owner_document = element->GetOwnerDocument();
  87. else
  88. owner_document = nullptr;
  89. lua_settop(L, top);
  90. }
  91. LuaEventListener::~LuaEventListener()
  92. {
  93. // Remove the Lua function from its table
  94. lua_State* L = Interpreter::GetLuaState();
  95. lua_getglobal(L, "EVENTLISTENERFUNCTIONS");
  96. luaL_unref(L, -1, luaFuncRef);
  97. lua_pop(L, 1); // pop table
  98. }
  99. void LuaEventListener::OnDetach(Element* /*element*/)
  100. {
  101. // We consider this listener owned by its element, so we must delete ourselves when
  102. // we detach (probably because element was removed).
  103. delete this;
  104. }
  105. /// Process the incoming Event
  106. void LuaEventListener::ProcessEvent(Event& event)
  107. {
  108. // not sure if this is the right place to do this, but if the element we are attached to isn't a document, then
  109. // the 'owner_document' variable will be nullptr, because element->ower_document hasn't been set on the construction. We should
  110. // correct that
  111. if (!owner_document && attached)
  112. owner_document = attached->GetOwnerDocument();
  113. lua_State* L = Interpreter::GetLuaState();
  114. int top = lua_gettop(L);
  115. // push the arguments
  116. lua_getglobal(L, "EVENTLISTENERFUNCTIONS");
  117. lua_rawgeti(L, -1, luaFuncRef);
  118. LuaType<Event>::push(L, &event, false);
  119. LuaType<Element>::push(L, attached, false);
  120. LuaType<Document>::push(L, owner_document, false);
  121. Interpreter::ExecuteCall(3, 0); // call the function at the top of the stack with 3 arguments
  122. lua_settop(L, top); // balanced stack makes Lua happy
  123. }
  124. } // namespace Lua
  125. } // namespace Rml