2
0

ContextDocumentsProxy.cpp 4.7 KB

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