Document.cpp 5.2 KB

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