Document.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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,"DocumentFocus");
  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,ElementDocument::NONE);
  50. lua_setfield(L,-2,"NONE");
  51. lua_pushinteger(L,ElementDocument::FOCUS);
  52. lua_setfield(L,-2,"FOCUS");
  53. lua_pushinteger(L,ElementDocument::MODAL);
  54. lua_setfield(L,-2,"MODAL");
  55. lua_setglobal(L,"DocumentFocus");
  56. }
  57. }
  58. //methods
  59. int DocumentPullToFront(lua_State* L, Document* obj)
  60. {
  61. obj->PullToFront();
  62. return 0;
  63. }
  64. int DocumentPushToBack(lua_State* L, Document* obj)
  65. {
  66. obj->PushToBack();
  67. return 0;
  68. }
  69. int DocumentShow(lua_State* L, Document* obj)
  70. {
  71. int top = lua_gettop(L);
  72. if(top == 0)
  73. obj->Show();
  74. else
  75. {
  76. int flag = luaL_checkinteger(L,1);
  77. obj->Show(flag);
  78. }
  79. return 0;
  80. }
  81. int DocumentHide(lua_State* L, Document* obj)
  82. {
  83. obj->Hide();
  84. return 0;
  85. }
  86. int DocumentClose(lua_State* L, Document* obj)
  87. {
  88. obj->Close();
  89. return 0;
  90. }
  91. int DocumentCreateElement(lua_State* L, Document* obj)
  92. {
  93. const char* tag = luaL_checkstring(L,1);
  94. ElementPtr* ele = new ElementPtr( obj->CreateElement(tag) );
  95. LuaType<ElementPtr>::push(L,ele,true);
  96. return 1;
  97. }
  98. int DocumentCreateTextNode(lua_State* L, Document* obj)
  99. {
  100. //need ElementText object first
  101. const char* text = luaL_checkstring(L,1);
  102. ElementPtr* et = new ElementPtr( obj->CreateTextNode(text) );
  103. LuaType<ElementPtr>::push(L, et, true);
  104. return 1;
  105. }
  106. //getters
  107. int DocumentGetAttrtitle(lua_State* L)
  108. {
  109. Document* doc = LuaType<Document>::check(L,1);
  110. LUACHECKOBJ(doc);
  111. lua_pushstring(L,doc->GetTitle().c_str());
  112. return 1;
  113. }
  114. int DocumentGetAttrcontext(lua_State* L)
  115. {
  116. Document* doc = LuaType<Document>::check(L,1);
  117. LUACHECKOBJ(doc);
  118. LuaType<Context>::push(L,doc->GetContext(),false);
  119. return 1;
  120. }
  121. //setters
  122. int DocumentSetAttrtitle(lua_State* L)
  123. {
  124. Document* doc = LuaType<Document>::check(L,1);
  125. LUACHECKOBJ(doc);
  126. const char* title = luaL_checkstring(L,2);
  127. doc->SetTitle(title);
  128. return 0;
  129. }
  130. RegType<Document> DocumentMethods[] =
  131. {
  132. LUAMETHOD(Document,PullToFront)
  133. LUAMETHOD(Document,PushToBack)
  134. LUAMETHOD(Document,Show)
  135. LUAMETHOD(Document,Hide)
  136. LUAMETHOD(Document,Close)
  137. LUAMETHOD(Document,CreateElement)
  138. LUAMETHOD(Document,CreateTextNode)
  139. { nullptr, nullptr },
  140. };
  141. luaL_Reg DocumentGetters[] =
  142. {
  143. LUAGETTER(Document,title)
  144. LUAGETTER(Document,context)
  145. { nullptr, nullptr },
  146. };
  147. luaL_Reg DocumentSetters[] =
  148. {
  149. LUASETTER(Document,title)
  150. { nullptr, nullptr },
  151. };
  152. LUACORETYPEDEFINE(Document)
  153. }
  154. }
  155. }