Context.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. Vector2i* dim = new Vector2i(cont->GetDimensions());
  127. LuaType<Vector2i>::push(L,dim,true);
  128. return 1;
  129. }
  130. //returns a table of everything
  131. int ContextGetAttrdocuments(lua_State* L)
  132. {
  133. Context* cont = LuaType<Context>::check(L,1);
  134. RMLUI_CHECK_OBJ(cont);
  135. ContextDocumentsProxy* cdp = new ContextDocumentsProxy();
  136. cdp->owner = cont;
  137. LuaType<ContextDocumentsProxy>::push(L,cdp,true); //does get garbage collected (deleted)
  138. return 1;
  139. }
  140. int ContextGetAttrfocus_element(lua_State* L)
  141. {
  142. Context* cont = LuaType<Context>::check(L,1);
  143. RMLUI_CHECK_OBJ(cont);
  144. LuaType<Element>::push(L,cont->GetFocusElement());
  145. return 1;
  146. }
  147. int ContextGetAttrhover_element(lua_State* L)
  148. {
  149. Context* cont = LuaType<Context>::check(L,1);
  150. RMLUI_CHECK_OBJ(cont);
  151. LuaType<Element>::push(L,cont->GetHoverElement());
  152. return 1;
  153. }
  154. int ContextGetAttrname(lua_State* L)
  155. {
  156. Context* cont = LuaType<Context>::check(L,1);
  157. RMLUI_CHECK_OBJ(cont);
  158. lua_pushstring(L,cont->GetName().c_str());
  159. return 1;
  160. }
  161. int ContextGetAttrroot_element(lua_State* L)
  162. {
  163. Context* cont = LuaType<Context>::check(L,1);
  164. RMLUI_CHECK_OBJ(cont);
  165. LuaType<Element>::push(L,cont->GetRootElement());
  166. return 1;
  167. }
  168. //setters
  169. int ContextSetAttrdimensions(lua_State* L)
  170. {
  171. Context* cont = LuaType<Context>::check(L,1);
  172. RMLUI_CHECK_OBJ(cont);
  173. Vector2i* dim = LuaType<Vector2i>::check(L,2);
  174. cont->SetDimensions(*dim);
  175. return 0;
  176. }
  177. RegType<Context> ContextMethods[] =
  178. {
  179. RMLUI_LUAMETHOD(Context,AddEventListener)
  180. RMLUI_LUAMETHOD(Context,CreateDocument)
  181. RMLUI_LUAMETHOD(Context,LoadDocument)
  182. RMLUI_LUAMETHOD(Context,Render)
  183. RMLUI_LUAMETHOD(Context,UnloadAllDocuments)
  184. RMLUI_LUAMETHOD(Context,UnloadDocument)
  185. RMLUI_LUAMETHOD(Context,Update)
  186. RMLUI_LUAMETHOD(Context,OpenDataModel)
  187. // todo: CloseDataModel
  188. { nullptr, nullptr },
  189. };
  190. luaL_Reg ContextGetters[] =
  191. {
  192. RMLUI_LUAGETTER(Context,dimensions)
  193. RMLUI_LUAGETTER(Context,documents)
  194. RMLUI_LUAGETTER(Context,focus_element)
  195. RMLUI_LUAGETTER(Context,hover_element)
  196. RMLUI_LUAGETTER(Context,name)
  197. RMLUI_LUAGETTER(Context,root_element)
  198. { nullptr, nullptr },
  199. };
  200. luaL_Reg ContextSetters[] =
  201. {
  202. RMLUI_LUASETTER(Context,dimensions)
  203. { nullptr, nullptr },
  204. };
  205. RMLUI_LUATYPE_DEFINE(Context)
  206. } // namespace Lua
  207. } // namespace Rml