ContextDocumentsProxy.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 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 "precompiled.h"
  29. #include "ContextDocumentsProxy.h"
  30. #include <RmlUi/Core/ElementDocument.h>
  31. namespace Rml {
  32. namespace Core {
  33. namespace Lua {
  34. typedef Rml::Core::ElementDocument Document;
  35. template<> void ExtraInit<ContextDocumentsProxy>(lua_State* L, int metatable_index)
  36. {
  37. lua_pushcfunction(L,ContextDocumentsProxy__index);
  38. lua_setfield(L,metatable_index,"__index");
  39. lua_pushcfunction(L,ContextDocumentsProxy__pairs);
  40. lua_setfield(L,metatable_index,"__pairs");
  41. lua_pushcfunction(L,ContextDocumentsProxy__ipairs);
  42. lua_setfield(L,metatable_index,"__ipairs");
  43. }
  44. int ContextDocumentsProxy__index(lua_State* L)
  45. {
  46. /*the table obj and the missing key are currently on the stack(index 1 & 2) as defined by the Lua language*/
  47. int type = lua_type(L,2);
  48. if(type == LUA_TNUMBER || type == LUA_TSTRING) //only valid key types
  49. {
  50. ContextDocumentsProxy* proxy = LuaType<ContextDocumentsProxy>::check(L,1);
  51. Document* ret = nullptr;
  52. if(type == LUA_TSTRING)
  53. ret = proxy->owner->GetDocument(luaL_checkstring(L,2));
  54. else
  55. ret = proxy->owner->GetDocument(luaL_checkinteger(L,2));
  56. LuaType<Document>::push(L,ret,false);
  57. return 1;
  58. }
  59. else
  60. return LuaType<ContextDocumentsProxy>::index(L);
  61. }
  62. //[1] is the object, [2] is the last used key, [3] is the userdata
  63. int ContextDocumentsProxy__pairs(lua_State* L)
  64. {
  65. Document* doc = nullptr;
  66. ContextDocumentsProxy* obj = LuaType<ContextDocumentsProxy>::check(L,1);
  67. LUACHECKOBJ(obj);
  68. int* pindex = (int*)lua_touserdata(L,3);
  69. if((*pindex) == -1)
  70. *pindex = 0;
  71. int num_docs = obj->owner->GetNumDocuments();
  72. //because there can be missing indexes, make sure to continue until there
  73. //is actually a document at the index
  74. while((*pindex) < num_docs)
  75. {
  76. doc = obj->owner->GetDocument((*pindex)++);
  77. if(doc != nullptr)
  78. break;
  79. }
  80. //If we found a document
  81. if(doc != nullptr)
  82. {
  83. lua_pushstring(L,doc->GetId().c_str());
  84. LuaType<Document>::push(L,doc);
  85. }
  86. else //if we were at the end and didn't find a document
  87. {
  88. lua_pushnil(L);
  89. lua_pushnil(L);
  90. }
  91. return 2;
  92. }
  93. //same as __pairs, but putting an integer key instead of a string key
  94. int ContextDocumentsProxy__ipairs(lua_State* L)
  95. {
  96. Document* doc = nullptr;
  97. ContextDocumentsProxy* obj = LuaType<ContextDocumentsProxy>::check(L,1);
  98. LUACHECKOBJ(obj);
  99. int* pindex = (int*)lua_touserdata(L,3);
  100. if((*pindex) == -1)
  101. *pindex = 0;
  102. int num_docs = obj->owner->GetNumDocuments();
  103. //because there can be missing indexes, make sure to continue until there
  104. //is actually a document at the index
  105. while((*pindex) < num_docs)
  106. {
  107. doc = obj->owner->GetDocument((*pindex)++);
  108. if(doc != nullptr)
  109. break;
  110. }
  111. //we found a document
  112. if(doc != nullptr)
  113. {
  114. lua_pushinteger(L,(*pindex)-1);
  115. LuaType<Document>::push(L,doc);
  116. }
  117. else //we got to the end and didn't find another document
  118. {
  119. lua_pushnil(L);
  120. lua_pushnil(L);
  121. }
  122. return 2;
  123. }
  124. RegType<ContextDocumentsProxy> ContextDocumentsProxyMethods[] =
  125. {
  126. { nullptr, nullptr },
  127. };
  128. luaL_Reg ContextDocumentsProxyGetters[] =
  129. {
  130. { nullptr, nullptr },
  131. };
  132. luaL_Reg ContextDocumentsProxySetters[] =
  133. {
  134. { nullptr, nullptr },
  135. };
  136. LUACORETYPEDEFINE(ContextDocumentsProxy)
  137. }
  138. }
  139. }