Context.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "precompiled.h"
  28. #include "Context.h"
  29. #include <Rocket/Core/Context.h>
  30. #include <Rocket/Core/ElementDocument.h>
  31. #include <Rocket/Core/Factory.h>
  32. #include "LuaEventListener.h"
  33. #include "ContextDocumentsProxy.h"
  34. namespace Rocket {
  35. namespace Core {
  36. namespace Lua {
  37. typedef Rocket::Core::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 = NULL;
  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 = 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,NULL), 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),NULL), 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 ContextAddMouseCursor(lua_State* L, Context* obj)
  76. {
  77. Document* cursor_doc = LuaType<Document>::check(L,1);
  78. obj->AddMouseCursor(cursor_doc);
  79. return 0;
  80. }
  81. int ContextCreateDocument(lua_State* L, Context* obj)
  82. {
  83. const char* tag;
  84. if(lua_gettop(L) < 1)
  85. tag = "body";
  86. else
  87. tag = luaL_checkstring(L,1);
  88. Document* doc = obj->CreateDocument(tag);
  89. LuaType<Document>::push(L,doc,true);
  90. //for debugging
  91. int count = doc->GetReferenceCount();
  92. return 1;
  93. }
  94. int ContextLoadDocument(lua_State* L, Context* obj)
  95. {
  96. const char* path = luaL_checkstring(L,1);
  97. Document* doc = obj->LoadDocument(path);
  98. LuaType<Document>::push(L,doc,false);
  99. doc->RemoveReference();
  100. return 1;
  101. }
  102. int ContextLoadMouseCursor(lua_State* L, Context* obj)
  103. {
  104. const char* path = luaL_checkstring(L,1);
  105. Document* doc = obj->LoadMouseCursor(path);
  106. LuaType<Document>::push(L,doc);
  107. return 1;
  108. }
  109. int ContextRender(lua_State* L, Context* obj)
  110. {
  111. lua_pushboolean(L,obj->Render());
  112. return 1;
  113. }
  114. int ContextShowMouseCursor(lua_State* L, Context* obj)
  115. {
  116. bool show = CHECK_BOOL(L,1);
  117. obj->ShowMouseCursor(show);
  118. return 0;
  119. }
  120. int ContextUnloadAllDocuments(lua_State* L, Context* obj)
  121. {
  122. obj->UnloadAllDocuments();
  123. return 0;
  124. }
  125. int ContextUnloadAllMouseCursors(lua_State* L, Context* obj)
  126. {
  127. obj->UnloadAllMouseCursors();
  128. return 0;
  129. }
  130. int ContextUnloadDocument(lua_State* L, Context* obj)
  131. {
  132. Document* doc = LuaType<Document>::check(L,1);
  133. obj->UnloadDocument(doc);
  134. return 0;
  135. }
  136. int ContextUnloadMouseCursor(lua_State* L, Context* obj)
  137. {
  138. const char* name = luaL_checkstring(L,1);
  139. obj->UnloadMouseCursor(name);
  140. return 0;
  141. }
  142. int ContextUpdate(lua_State* L, Context* obj)
  143. {
  144. lua_pushboolean(L,obj->Update());
  145. return 1;
  146. }
  147. //getters
  148. int ContextGetAttrdimensions(lua_State* L)
  149. {
  150. Context* cont = LuaType<Context>::check(L,1);
  151. const Vector2i* dim = &cont->GetDimensions();
  152. //const_cast-ing so that the user can do dimensions.x = 3 and it will actually change the dimensions
  153. //of the context
  154. LuaType<Vector2i>::push(L,const_cast<Vector2i*>(dim));
  155. return 1;
  156. }
  157. //returns a table of everything
  158. int ContextGetAttrdocuments(lua_State* L)
  159. {
  160. Context* cont = LuaType<Context>::check(L,1);
  161. LUACHECKOBJ(cont);
  162. ContextDocumentsProxy* cdp = new ContextDocumentsProxy();
  163. cdp->owner = cont;
  164. LuaType<ContextDocumentsProxy>::push(L,cdp,true); //does get garbage collected (deleted)
  165. return 1;
  166. }
  167. int ContextGetAttrfocus_element(lua_State* L)
  168. {
  169. Context* cont = LuaType<Context>::check(L,1);
  170. LUACHECKOBJ(cont);
  171. LuaType<Element>::push(L,cont->GetFocusElement());
  172. return 1;
  173. }
  174. int ContextGetAttrhover_element(lua_State* L)
  175. {
  176. Context* cont = LuaType<Context>::check(L,1);
  177. LUACHECKOBJ(cont);
  178. LuaType<Element>::push(L,cont->GetHoverElement());
  179. return 1;
  180. }
  181. int ContextGetAttrname(lua_State* L)
  182. {
  183. Context* cont = LuaType<Context>::check(L,1);
  184. LUACHECKOBJ(cont);
  185. lua_pushstring(L,cont->GetName().CString());
  186. return 1;
  187. }
  188. int ContextGetAttrroot_element(lua_State* L)
  189. {
  190. Context* cont = LuaType<Context>::check(L,1);
  191. LUACHECKOBJ(cont);
  192. LuaType<Element>::push(L,cont->GetRootElement());
  193. return 1;
  194. }
  195. //setters
  196. int ContextSetAttrdimensions(lua_State* L)
  197. {
  198. Context* cont = LuaType<Context>::check(L,1);
  199. LUACHECKOBJ(cont);
  200. Vector2i* dim = LuaType<Vector2i>::check(L,2);
  201. cont->SetDimensions(*dim);
  202. return 0;
  203. }
  204. RegType<Context> ContextMethods[] =
  205. {
  206. LUAMETHOD(Context,AddEventListener)
  207. LUAMETHOD(Context,AddMouseCursor)
  208. LUAMETHOD(Context,CreateDocument)
  209. LUAMETHOD(Context,LoadDocument)
  210. LUAMETHOD(Context,LoadMouseCursor)
  211. LUAMETHOD(Context,Render)
  212. LUAMETHOD(Context,ShowMouseCursor)
  213. LUAMETHOD(Context,UnloadAllDocuments)
  214. LUAMETHOD(Context,UnloadAllMouseCursors)
  215. LUAMETHOD(Context,UnloadDocument)
  216. LUAMETHOD(Context,UnloadMouseCursor)
  217. LUAMETHOD(Context,Update)
  218. { NULL, NULL },
  219. };
  220. luaL_reg ContextGetters[] =
  221. {
  222. LUAGETTER(Context,dimensions)
  223. LUAGETTER(Context,documents)
  224. LUAGETTER(Context,focus_element)
  225. LUAGETTER(Context,hover_element)
  226. LUAGETTER(Context,name)
  227. LUAGETTER(Context,root_element)
  228. { NULL, NULL },
  229. };
  230. luaL_reg ContextSetters[] =
  231. {
  232. LUASETTER(Context,dimensions)
  233. { NULL, NULL },
  234. };
  235. LUATYPEDEFINE(Context,true)
  236. }
  237. }
  238. }