Context.cpp 6.5 KB

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