Context.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. //methods
  39. int ContextAddEventListener(lua_State* L, Context* obj)
  40. {
  41. //need to make an EventListener for Lua before I can do anything else
  42. const char* evt = luaL_checkstring(L,1); //event
  43. Element* element = NULL;
  44. bool capturephase = false;
  45. //get the rest of the stuff needed to construct the listener
  46. if(lua_gettop(L) > 2)
  47. {
  48. if(!lua_isnoneornil(L,3))
  49. element = LuaType<Element>::check(L,3);
  50. if(!lua_isnoneornil(L,4))
  51. capturephase = CHECK_BOOL(L,4);
  52. }
  53. int type = lua_type(L,2);
  54. if(type == LUA_TFUNCTION)
  55. {
  56. if(element)
  57. element->AddEventListener(evt, new LuaEventListener(L,2,element), capturephase);
  58. else
  59. obj->AddEventListener(evt, new LuaEventListener(L,2,NULL), capturephase);
  60. }
  61. else if(type == LUA_TSTRING)
  62. {
  63. if(element)
  64. element->AddEventListener(evt, new LuaEventListener(luaL_checkstring(L,2),element), capturephase);
  65. else
  66. obj->AddEventListener(evt, new LuaEventListener(luaL_checkstring(L,2),NULL), capturephase);
  67. }
  68. else
  69. {
  70. 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));
  71. }
  72. return 0;
  73. }
  74. int ContextAddMouseCursor(lua_State* L, Context* obj)
  75. {
  76. Document* cursor_doc = LuaType<Document>::check(L,1);
  77. obj->AddMouseCursor(cursor_doc);
  78. return 0;
  79. }
  80. int ContextCreateDocument(lua_State* L, Context* obj)
  81. {
  82. const char* tag;
  83. if(lua_gettop(L) < 1)
  84. tag = "body";
  85. else
  86. tag = luaL_checkstring(L,1);
  87. Document* doc = obj->CreateDocument(tag);
  88. LuaType<Document>::push(L,doc,true);
  89. //for debugging
  90. int count = doc->GetReferenceCount();
  91. return 1;
  92. }
  93. int ContextLoadDocument(lua_State* L, Context* obj)
  94. {
  95. const char* path = luaL_checkstring(L,1);
  96. Document* doc = obj->LoadDocument(path);
  97. LuaType<Document>::push(L,doc,false);
  98. doc->RemoveReference();
  99. return 1;
  100. }
  101. int ContextLoadMouseCursor(lua_State* L, Context* obj)
  102. {
  103. const char* path = luaL_checkstring(L,1);
  104. Document* doc = obj->LoadMouseCursor(path);
  105. LuaType<Document>::push(L,doc);
  106. return 1;
  107. }
  108. int ContextRender(lua_State* L, Context* obj)
  109. {
  110. lua_pushboolean(L,obj->Render());
  111. return 1;
  112. }
  113. int ContextShowMouseCursor(lua_State* L, Context* obj)
  114. {
  115. bool show = CHECK_BOOL(L,1);
  116. obj->ShowMouseCursor(show);
  117. return 0;
  118. }
  119. int ContextUnloadAllDocuments(lua_State* L, Context* obj)
  120. {
  121. obj->UnloadAllDocuments();
  122. return 0;
  123. }
  124. int ContextUnloadAllMouseCursors(lua_State* L, Context* obj)
  125. {
  126. obj->UnloadAllMouseCursors();
  127. return 0;
  128. }
  129. int ContextUnloadDocument(lua_State* L, Context* obj)
  130. {
  131. Document* doc = LuaType<Document>::check(L,1);
  132. obj->UnloadDocument(doc);
  133. return 0;
  134. }
  135. int ContextUnloadMouseCursor(lua_State* L, Context* obj)
  136. {
  137. const char* name = luaL_checkstring(L,1);
  138. obj->UnloadMouseCursor(name);
  139. return 0;
  140. }
  141. int ContextUpdate(lua_State* L, Context* obj)
  142. {
  143. lua_pushboolean(L,obj->Update());
  144. return 1;
  145. }
  146. //getters
  147. int ContextGetAttrdimensions(lua_State* L)
  148. {
  149. Context* cont = LuaType<Context>::check(L,1);
  150. const Vector2i* dim = &cont->GetDimensions();
  151. //const_cast-ing so that the user can do dimensions.x = 3 and it will actually change the dimensions
  152. //of the context
  153. LuaType<Vector2i>::push(L,const_cast<Vector2i*>(dim));
  154. return 1;
  155. }
  156. //returns a table of everything
  157. int ContextGetAttrdocuments(lua_State* L)
  158. {
  159. Context* cont = LuaType<Context>::check(L,1);
  160. LUACHECKOBJ(cont);
  161. ContextDocumentsProxy* cdp = new ContextDocumentsProxy();
  162. cdp->owner = cont;
  163. LuaType<ContextDocumentsProxy>::push(L,cdp,true); //does get garbage collected (deleted)
  164. return 1;
  165. }
  166. int ContextGetAttrfocus_element(lua_State* L)
  167. {
  168. Context* cont = LuaType<Context>::check(L,1);
  169. LUACHECKOBJ(cont);
  170. LuaType<Element>::push(L,cont->GetFocusElement());
  171. return 1;
  172. }
  173. int ContextGetAttrhover_element(lua_State* L)
  174. {
  175. Context* cont = LuaType<Context>::check(L,1);
  176. LUACHECKOBJ(cont);
  177. LuaType<Element>::push(L,cont->GetHoverElement());
  178. return 1;
  179. }
  180. int ContextGetAttrname(lua_State* L)
  181. {
  182. Context* cont = LuaType<Context>::check(L,1);
  183. LUACHECKOBJ(cont);
  184. lua_pushstring(L,cont->GetName().CString());
  185. return 1;
  186. }
  187. int ContextGetAttrroot_element(lua_State* L)
  188. {
  189. Context* cont = LuaType<Context>::check(L,1);
  190. LUACHECKOBJ(cont);
  191. LuaType<Element>::push(L,cont->GetRootElement());
  192. return 1;
  193. }
  194. //setters
  195. int ContextSetAttrdimensions(lua_State* L)
  196. {
  197. Context* cont = LuaType<Context>::check(L,1);
  198. LUACHECKOBJ(cont);
  199. Vector2i* dim = LuaType<Vector2i>::check(L,2);
  200. cont->SetDimensions(*dim);
  201. return 0;
  202. }
  203. RegType<Context> ContextMethods[] =
  204. {
  205. LUAMETHOD(Context,AddEventListener)
  206. LUAMETHOD(Context,AddMouseCursor)
  207. LUAMETHOD(Context,CreateDocument)
  208. LUAMETHOD(Context,LoadDocument)
  209. LUAMETHOD(Context,LoadMouseCursor)
  210. LUAMETHOD(Context,Render)
  211. LUAMETHOD(Context,ShowMouseCursor)
  212. LUAMETHOD(Context,UnloadAllDocuments)
  213. LUAMETHOD(Context,UnloadAllMouseCursors)
  214. LUAMETHOD(Context,UnloadDocument)
  215. LUAMETHOD(Context,UnloadMouseCursor)
  216. LUAMETHOD(Context,Update)
  217. { NULL, NULL },
  218. };
  219. luaL_reg ContextGetters[] =
  220. {
  221. LUAGETTER(Context,dimensions)
  222. LUAGETTER(Context,documents)
  223. LUAGETTER(Context,focus_element)
  224. LUAGETTER(Context,hover_element)
  225. LUAGETTER(Context,name)
  226. LUAGETTER(Context,root_element)
  227. { NULL, NULL },
  228. };
  229. luaL_reg ContextSetters[] =
  230. {
  231. LUASETTER(Context,dimensions)
  232. { NULL, NULL },
  233. };
  234. /*
  235. template<> const char* GetTClassName<Context>() { return "Context"; }
  236. template<> RegType<Context>* GetMethodTable<Context>() { return ContextMethods; }
  237. template<> luaL_reg* GetAttrTable<Context>() { return ContextGetters; }
  238. template<> luaL_reg* SetAttrTable<Context>() { return ContextSetters; }
  239. */
  240. }
  241. }
  242. }