Document.cpp 5.3 KB

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