Document.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 "Document.h"
  29. #include "Element.h"
  30. #include <RmlUi/Core/Context.h>
  31. #include <RmlUi/Core/ElementDocument.h>
  32. #include <RmlUi/Lua/Utilities.h>
  33. namespace Rml {
  34. namespace Lua {
  35. template <>
  36. void ExtraInit<Document>(lua_State* L, int metatable_index)
  37. {
  38. // we will inherit from Element
  39. ExtraInit<Element>(L, metatable_index);
  40. LuaType<Element>::_regfunctions(L, metatable_index, metatable_index - 1);
  41. AddTypeToElementAsTable<Document>(L);
  42. // create the DocumentFocus table
  43. lua_getglobal(L, "DocumentModal");
  44. if (lua_isnoneornil(L, -1))
  45. {
  46. lua_pop(L, 1); // pop unsucessful getglobal
  47. lua_newtable(L); // create a table for holding the enum
  48. lua_pushinteger(L, (int)ModalFlag::None);
  49. lua_setfield(L, -2, "None");
  50. lua_pushinteger(L, (int)ModalFlag::Modal);
  51. lua_setfield(L, -2, "Modal");
  52. lua_pushinteger(L, (int)ModalFlag::Keep);
  53. lua_setfield(L, -2, "Keep");
  54. lua_setglobal(L, "DocumentModal");
  55. }
  56. // create the DocumentFocus table
  57. lua_getglobal(L, "DocumentFocus");
  58. if (lua_isnoneornil(L, -1))
  59. {
  60. lua_pop(L, 1); // pop unsucessful getglobal
  61. lua_newtable(L); // create a table for holding the enum
  62. lua_pushinteger(L, (int)FocusFlag::None);
  63. lua_setfield(L, -2, "None");
  64. lua_pushinteger(L, (int)FocusFlag::Document);
  65. lua_setfield(L, -2, "Document");
  66. lua_pushinteger(L, (int)FocusFlag::Keep);
  67. lua_setfield(L, -2, "Keep");
  68. lua_pushinteger(L, (int)FocusFlag::Auto);
  69. lua_setfield(L, -2, "Auto");
  70. lua_setglobal(L, "DocumentFocus");
  71. }
  72. }
  73. // methods
  74. int DocumentPullToFront(lua_State* /*L*/, Document* obj)
  75. {
  76. obj->PullToFront();
  77. return 0;
  78. }
  79. int DocumentPushToBack(lua_State* /*L*/, Document* obj)
  80. {
  81. obj->PushToBack();
  82. return 0;
  83. }
  84. int DocumentShow(lua_State* L, Document* obj)
  85. {
  86. int top = lua_gettop(L);
  87. if (top == 0)
  88. obj->Show();
  89. else if (top == 1)
  90. {
  91. ModalFlag modal = (ModalFlag)luaL_checkinteger(L, 1);
  92. obj->Show(modal);
  93. }
  94. else
  95. {
  96. ModalFlag modal = (ModalFlag)luaL_checkinteger(L, 1);
  97. FocusFlag focus = (FocusFlag)luaL_checkinteger(L, 2);
  98. obj->Show(modal, focus);
  99. }
  100. return 0;
  101. }
  102. int DocumentHide(lua_State* /*L*/, Document* obj)
  103. {
  104. obj->Hide();
  105. return 0;
  106. }
  107. int DocumentClose(lua_State* /*L*/, Document* obj)
  108. {
  109. obj->Close();
  110. return 0;
  111. }
  112. int DocumentCreateElement(lua_State* L, Document* obj)
  113. {
  114. const char* tag = luaL_checkstring(L, 1);
  115. ElementPtr* ele = new ElementPtr(obj->CreateElement(tag));
  116. LuaType<ElementPtr>::push(L, ele, true);
  117. return 1;
  118. }
  119. int DocumentCreateTextNode(lua_State* L, Document* obj)
  120. {
  121. // need ElementText object first
  122. const char* text = luaL_checkstring(L, 1);
  123. ElementPtr* et = new ElementPtr(obj->CreateTextNode(text));
  124. LuaType<ElementPtr>::push(L, et, true);
  125. return 1;
  126. }
  127. // getters
  128. int DocumentGetAttrtitle(lua_State* L)
  129. {
  130. Document* doc = LuaType<Document>::check(L, 1);
  131. RMLUI_CHECK_OBJ(doc);
  132. lua_pushstring(L, doc->GetTitle().c_str());
  133. return 1;
  134. }
  135. int DocumentGetAttrcontext(lua_State* L)
  136. {
  137. Document* doc = LuaType<Document>::check(L, 1);
  138. RMLUI_CHECK_OBJ(doc);
  139. LuaType<Context>::push(L, doc->GetContext(), false);
  140. return 1;
  141. }
  142. // setters
  143. int DocumentSetAttrtitle(lua_State* L)
  144. {
  145. Document* doc = LuaType<Document>::check(L, 1);
  146. RMLUI_CHECK_OBJ(doc);
  147. const char* title = luaL_checkstring(L, 2);
  148. doc->SetTitle(title);
  149. return 0;
  150. }
  151. RegType<Document> DocumentMethods[] = {
  152. RMLUI_LUAMETHOD(Document, PullToFront),
  153. RMLUI_LUAMETHOD(Document, PushToBack),
  154. RMLUI_LUAMETHOD(Document, Show),
  155. RMLUI_LUAMETHOD(Document, Hide),
  156. RMLUI_LUAMETHOD(Document, Close),
  157. RMLUI_LUAMETHOD(Document, CreateElement),
  158. RMLUI_LUAMETHOD(Document, CreateTextNode),
  159. {nullptr, nullptr},
  160. };
  161. luaL_Reg DocumentGetters[] = {
  162. RMLUI_LUAGETTER(Document, title),
  163. RMLUI_LUAGETTER(Document, context),
  164. {nullptr, nullptr},
  165. };
  166. luaL_Reg DocumentSetters[] = {
  167. RMLUI_LUASETTER(Document, title),
  168. {nullptr, nullptr},
  169. };
  170. RMLUI_LUATYPE_DEFINE(Document)
  171. } // namespace Lua
  172. } // namespace Rml