Context.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 "Context.h"
  30. #include <RmlUi/Core/Context.h>
  31. #include <RmlUi/Core/ElementDocument.h>
  32. #include <RmlUi/Core/Factory.h>
  33. #include "LuaEventListener.h"
  34. #include "ContextDocumentsProxy.h"
  35. namespace Rml {
  36. namespace Core {
  37. namespace Lua {
  38. typedef Rml::Core::ElementDocument Document;
  39. template<> void ExtraInit<Context>(lua_State* /*L*/, int /*metatable_index*/) { return; }
  40. //methods
  41. int ContextAddEventListener(lua_State* L, Context* obj)
  42. {
  43. //need to make an EventListener for Lua before I can do anything else
  44. const char* evt = luaL_checkstring(L,1); //event
  45. Element* element = nullptr;
  46. bool capturephase = false;
  47. //get the rest of the stuff needed to construct the listener
  48. if(lua_gettop(L) > 2)
  49. {
  50. if(!lua_isnoneornil(L,3))
  51. element = LuaType<Element>::check(L,3);
  52. if(!lua_isnoneornil(L,4))
  53. capturephase = CHECK_BOOL(L,4);
  54. }
  55. int type = lua_type(L,2);
  56. if(type == LUA_TFUNCTION)
  57. {
  58. if(element)
  59. element->AddEventListener(evt, new LuaEventListener(L,2,element), capturephase);
  60. else
  61. obj->AddEventListener(evt, new LuaEventListener(L,2,nullptr), capturephase);
  62. }
  63. else if(type == LUA_TSTRING)
  64. {
  65. if(element)
  66. element->AddEventListener(evt, new LuaEventListener(luaL_checkstring(L,2),element), capturephase);
  67. else
  68. obj->AddEventListener(evt, new LuaEventListener(luaL_checkstring(L,2),nullptr), capturephase);
  69. }
  70. else
  71. {
  72. Log::Message(Log::LT_WARNING, "Lua Context:AddEventLisener's 2nd argument can only be a Lua function or a string, you passed in a %s", lua_typename(L,type));
  73. }
  74. return 0;
  75. }
  76. int ContextCreateDocument(lua_State* L, Context* obj)
  77. {
  78. const char* tag;
  79. if(lua_gettop(L) < 1)
  80. tag = "body";
  81. else
  82. tag = luaL_checkstring(L,1);
  83. Document* doc = obj->CreateDocument(tag);
  84. LuaType<Document>::push(L,doc,false);
  85. return 1;
  86. }
  87. int ContextLoadDocument(lua_State* L, Context* obj)
  88. {
  89. const char* path = luaL_checkstring(L,1);
  90. Document* doc = obj->LoadDocument(path);
  91. LuaType<Document>::push(L,doc,false);
  92. return 1;
  93. }
  94. int ContextRender(lua_State* L, Context* obj)
  95. {
  96. lua_pushboolean(L,obj->Render());
  97. return 1;
  98. }
  99. int ContextUnloadAllDocuments(lua_State* /*L*/, Context* obj)
  100. {
  101. obj->UnloadAllDocuments();
  102. return 0;
  103. }
  104. int ContextUnloadDocument(lua_State* L, Context* obj)
  105. {
  106. Document* doc = LuaType<Document>::check(L,1);
  107. obj->UnloadDocument(doc);
  108. return 0;
  109. }
  110. int ContextUpdate(lua_State* L, Context* obj)
  111. {
  112. lua_pushboolean(L,obj->Update());
  113. return 1;
  114. }
  115. //getters
  116. int ContextGetAttrdimensions(lua_State* L)
  117. {
  118. Context* cont = LuaType<Context>::check(L,1);
  119. const Vector2i* dim = &cont->GetDimensions();
  120. //const_cast-ing so that the user can do dimensions.x = 3 and it will actually change the dimensions
  121. //of the context
  122. LuaType<Vector2i>::push(L,const_cast<Vector2i*>(dim));
  123. return 1;
  124. }
  125. //returns a table of everything
  126. int ContextGetAttrdocuments(lua_State* L)
  127. {
  128. Context* cont = LuaType<Context>::check(L,1);
  129. LUACHECKOBJ(cont);
  130. ContextDocumentsProxy* cdp = new ContextDocumentsProxy();
  131. cdp->owner = cont;
  132. LuaType<ContextDocumentsProxy>::push(L,cdp,true); //does get garbage collected (deleted)
  133. return 1;
  134. }
  135. int ContextGetAttrfocus_element(lua_State* L)
  136. {
  137. Context* cont = LuaType<Context>::check(L,1);
  138. LUACHECKOBJ(cont);
  139. LuaType<Element>::push(L,cont->GetFocusElement());
  140. return 1;
  141. }
  142. int ContextGetAttrhover_element(lua_State* L)
  143. {
  144. Context* cont = LuaType<Context>::check(L,1);
  145. LUACHECKOBJ(cont);
  146. LuaType<Element>::push(L,cont->GetHoverElement());
  147. return 1;
  148. }
  149. int ContextGetAttrname(lua_State* L)
  150. {
  151. Context* cont = LuaType<Context>::check(L,1);
  152. LUACHECKOBJ(cont);
  153. lua_pushstring(L,cont->GetName().c_str());
  154. return 1;
  155. }
  156. int ContextGetAttrroot_element(lua_State* L)
  157. {
  158. Context* cont = LuaType<Context>::check(L,1);
  159. LUACHECKOBJ(cont);
  160. LuaType<Element>::push(L,cont->GetRootElement());
  161. return 1;
  162. }
  163. //setters
  164. int ContextSetAttrdimensions(lua_State* L)
  165. {
  166. Context* cont = LuaType<Context>::check(L,1);
  167. LUACHECKOBJ(cont);
  168. Vector2i* dim = LuaType<Vector2i>::check(L,2);
  169. cont->SetDimensions(*dim);
  170. return 0;
  171. }
  172. RegType<Context> ContextMethods[] =
  173. {
  174. LUAMETHOD(Context,AddEventListener)
  175. LUAMETHOD(Context,CreateDocument)
  176. LUAMETHOD(Context,LoadDocument)
  177. LUAMETHOD(Context,Render)
  178. LUAMETHOD(Context,UnloadAllDocuments)
  179. LUAMETHOD(Context,UnloadDocument)
  180. LUAMETHOD(Context,Update)
  181. { nullptr, nullptr },
  182. };
  183. luaL_Reg ContextGetters[] =
  184. {
  185. LUAGETTER(Context,dimensions)
  186. LUAGETTER(Context,documents)
  187. LUAGETTER(Context,focus_element)
  188. LUAGETTER(Context,hover_element)
  189. LUAGETTER(Context,name)
  190. LUAGETTER(Context,root_element)
  191. { nullptr, nullptr },
  192. };
  193. luaL_Reg ContextSetters[] =
  194. {
  195. LUASETTER(Context,dimensions)
  196. { nullptr, nullptr },
  197. };
  198. LUACORETYPEDEFINE(Context)
  199. }
  200. }
  201. }