Element.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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 "Element.h"
  29. #include <ElementStyle.h>
  30. #include "LuaEventListener.h"
  31. #include "ElementAttributesProxy.h"
  32. #include "ElementChildNodesProxy.h"
  33. #include <Rocket/Core/Lua/Utilities.h>
  34. namespace Rocket {
  35. namespace Core {
  36. namespace Lua {
  37. typedef ElementDocument Document;
  38. template<> void ExtraInit<Element>(lua_State* L, int metatable_index)
  39. {
  40. int top = lua_gettop(L);
  41. //guarantee the "Element.As" table exists
  42. lua_getfield(L,metatable_index-1,"As");
  43. if(lua_isnoneornil(L,-1)) //if it doesn't exist, create it
  44. {
  45. lua_newtable(L);
  46. lua_setfield(L,metatable_index-1,"As");
  47. }
  48. lua_pop(L,1); //pop the result of lua_getfield
  49. lua_pushcfunction(L,Elementnew);
  50. lua_setfield(L,metatable_index-1,"new");
  51. lua_settop(L,top);
  52. }
  53. int Elementnew(lua_State* L)
  54. {
  55. const char* tag = luaL_checkstring(L,1);
  56. Element* ele = new Element(tag);
  57. LuaType<Element>::push(L,ele,true);
  58. ele->RemoveReference();
  59. return 1;
  60. }
  61. //methods
  62. int ElementAddEventListener(lua_State* L, Element* obj)
  63. {
  64. int top = lua_gettop(L);
  65. bool capture = false;
  66. //default false if they didn't pass it in
  67. if (top > 2)
  68. capture = CHECK_BOOL(L,3);
  69. const char* event = luaL_checkstring(L,1);
  70. LuaEventListener* listener = NULL;
  71. int type = lua_type(L,2);
  72. if(type == LUA_TFUNCTION)
  73. {
  74. listener = new LuaEventListener(L,2,obj);
  75. }
  76. else if(type == LUA_TSTRING)
  77. {
  78. const char* code = luaL_checkstring(L,2);
  79. listener = new LuaEventListener(code,obj);
  80. }
  81. else
  82. {
  83. 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));
  84. }
  85. if(listener != NULL)
  86. {
  87. obj->AddEventListener(event,listener,capture);
  88. }
  89. return 0;
  90. }
  91. int ElementAppendChild(lua_State* L, Element* obj)
  92. {
  93. Element* ele = LuaType<Element>::check(L,1);
  94. obj->AppendChild(ele);
  95. return 0;
  96. }
  97. int ElementBlur(lua_State* L, Element* obj)
  98. {
  99. obj->Blur();
  100. return 0;
  101. }
  102. int ElementClick(lua_State* L, Element* obj)
  103. {
  104. obj->Click();
  105. return 0;
  106. }
  107. int ElementDispatchEvent(lua_State* L, Element* obj)
  108. {
  109. const char* event = luaL_checkstring(L,1);
  110. Dictionary params;
  111. lua_pushnil(L); //becauase lua_next pops a key from the stack first, we don't want to pop the table
  112. while(lua_next(L,2) != 0)
  113. {
  114. //[-1] is value, [-2] is key
  115. int type = lua_type(L,-1);
  116. const char* key = luaL_checkstring(L,-2); //key HAS to be a string, or things will go bad
  117. switch(type)
  118. {
  119. case LUA_TNUMBER:
  120. params.Set(key,(float)lua_tonumber(L,-1));
  121. break;
  122. case LUA_TBOOLEAN:
  123. params.Set(key,CHECK_BOOL(L,-1));
  124. break;
  125. case LUA_TSTRING:
  126. params.Set(key,luaL_checkstring(L,-1));
  127. break;
  128. case LUA_TUSERDATA:
  129. case LUA_TLIGHTUSERDATA:
  130. params.Set(key,lua_touserdata(L,-1));
  131. break;
  132. default:
  133. break;
  134. }
  135. }
  136. obj->DispatchEvent(event,params,false);
  137. return 0;
  138. }
  139. int ElementFocus(lua_State* L, Element* obj)
  140. {
  141. obj->Focus();
  142. return 0;
  143. }
  144. int ElementGetAttribute(lua_State* L, Element* obj)
  145. {
  146. const char* name = luaL_checkstring(L,1);
  147. Variant* var = obj->GetAttribute(name);
  148. PushVariant(L,var);
  149. return 1;
  150. }
  151. int ElementGetElementById(lua_State* L, Element* obj)
  152. {
  153. const char* id = luaL_checkstring(L,1);
  154. Element* ele = obj->GetElementById(id);
  155. LuaType<Element>::push(L,ele,false);
  156. return 1;
  157. }
  158. int ElementGetElementsByTagName(lua_State* L, Element* obj)
  159. {
  160. const char* tag = luaL_checkstring(L,1);
  161. ElementList list;
  162. obj->GetElementsByTagName(list,tag);
  163. lua_newtable(L);
  164. for(unsigned int i = 0; i < list.size(); i++)
  165. {
  166. lua_pushinteger(L,i);
  167. LuaType<Element>::push(L,list[i],false);
  168. lua_settable(L,-3); //-3 is the table
  169. }
  170. return 1;
  171. }
  172. int ElementHasAttribute(lua_State* L, Element* obj)
  173. {
  174. const char* name = luaL_checkstring(L,1);
  175. lua_pushboolean(L,obj->HasAttribute(name));
  176. return 1;
  177. }
  178. int ElementHasChildNodes(lua_State* L, Element* obj)
  179. {
  180. lua_pushboolean(L,obj->HasChildNodes());
  181. return 1;
  182. }
  183. int ElementInsertBefore(lua_State* L, Element* obj)
  184. {
  185. Element* element = LuaType<Element>::check(L,1);
  186. Element* adjacent = LuaType<Element>::check(L,2);
  187. obj->InsertBefore(element,adjacent);
  188. return 0;
  189. }
  190. int ElementIsClassSet(lua_State* L, Element* obj)
  191. {
  192. const char* name = luaL_checkstring(L,1);
  193. lua_pushboolean(L,obj->IsClassSet(name));
  194. return 1;
  195. }
  196. int ElementRemoveAttribute(lua_State* L, Element* obj)
  197. {
  198. const char* name = luaL_checkstring(L,1);
  199. obj->RemoveAttribute(name);
  200. return 0;
  201. }
  202. int ElementRemoveChild(lua_State* L, Element* obj)
  203. {
  204. Element* element = LuaType<Element>::check(L,1);
  205. lua_pushboolean(L,obj->RemoveChild(element));
  206. return 1;
  207. }
  208. int ElementReplaceChild(lua_State* L, Element* obj)
  209. {
  210. Element* inserted = LuaType<Element>::check(L,1);
  211. Element* replaced = LuaType<Element>::check(L,2);
  212. lua_pushboolean(L,obj->ReplaceChild(inserted,replaced));
  213. return 1;
  214. }
  215. int ElementScrollIntoView(lua_State* L, Element* obj)
  216. {
  217. bool align = CHECK_BOOL(L,1);
  218. obj->ScrollIntoView(align);
  219. return 0;
  220. }
  221. int ElementSetAttribute(lua_State* L, Element* obj)
  222. {
  223. const char* name = luaL_checkstring(L,1);
  224. const char* value = luaL_checkstring(L,2);
  225. obj->SetAttribute(name,String(value));
  226. return 0;
  227. }
  228. int ElementSetClass(lua_State* L, Element* obj)
  229. {
  230. const char* name = luaL_checkstring(L,1);
  231. bool value = CHECK_BOOL(L,2);
  232. obj->SetClass(name,value);
  233. return 0;
  234. }
  235. //getters
  236. int ElementGetAttrattributes(lua_State* L)
  237. {
  238. Element* ele = LuaType<Element>::check(L,1);
  239. LUACHECKOBJ(ele);
  240. ElementAttributesProxy* proxy = new ElementAttributesProxy();
  241. proxy->owner = ele;
  242. LuaType<ElementAttributesProxy>::push(L,proxy,true);
  243. return 1;
  244. }
  245. int ElementGetAttrchild_nodes(lua_State* L)
  246. {
  247. Element* ele = LuaType<Element>::check(L,1);
  248. LUACHECKOBJ(ele);
  249. ElementChildNodesProxy* ecnp = new ElementChildNodesProxy();
  250. ecnp->owner = ele;
  251. LuaType<ElementChildNodesProxy>::push(L,ecnp,true);
  252. return 1;
  253. }
  254. int ElementGetAttrclass_name(lua_State* L)
  255. {
  256. Element* ele = LuaType<Element>::check(L,1);
  257. LUACHECKOBJ(ele);
  258. const char* classnames = ele->GetClassNames().CString();
  259. lua_pushstring(L,classnames);
  260. return 1;
  261. }
  262. int ElementGetAttrclient_left(lua_State* L)
  263. {
  264. Element* ele = LuaType<Element>::check(L,1);
  265. LUACHECKOBJ(ele);
  266. lua_pushnumber(L,ele->GetClientLeft());
  267. return 1;
  268. }
  269. int ElementGetAttrclient_height(lua_State* L)
  270. {
  271. Element* ele = LuaType<Element>::check(L,1);
  272. LUACHECKOBJ(ele);
  273. lua_pushnumber(L,ele->GetClientHeight());
  274. return 1;
  275. }
  276. int ElementGetAttrclient_top(lua_State* L)
  277. {
  278. Element* ele = LuaType<Element>::check(L,1);
  279. LUACHECKOBJ(ele);
  280. lua_pushnumber(L,ele->GetClientTop());
  281. return 1;
  282. }
  283. int ElementGetAttrclient_width(lua_State* L)
  284. {
  285. Element* ele = LuaType<Element>::check(L,1);
  286. LUACHECKOBJ(ele);
  287. lua_pushnumber(L,ele->GetClientWidth());
  288. return 1;
  289. }
  290. int ElementGetAttrfirst_child(lua_State* L)
  291. {
  292. Element* ele = LuaType<Element>::check(L,1);
  293. LUACHECKOBJ(ele);
  294. Element* child = ele->GetFirstChild();
  295. if(child == NULL)
  296. lua_pushnil(L);
  297. else
  298. LuaType<Element>::push(L,child,false);
  299. return 1;
  300. }
  301. int ElementGetAttrid(lua_State* L)
  302. {
  303. Element* ele = LuaType<Element>::check(L,1);
  304. LUACHECKOBJ(ele);
  305. lua_pushstring(L,ele->GetId().CString());
  306. return 1;
  307. }
  308. int ElementGetAttrinner_rml(lua_State* L)
  309. {
  310. Element* ele = LuaType<Element>::check(L,1);
  311. LUACHECKOBJ(ele);
  312. lua_pushstring(L,ele->GetInnerRML().CString());
  313. return 1;
  314. }
  315. int ElementGetAttrlast_child(lua_State* L)
  316. {
  317. Element* ele = LuaType<Element>::check(L,1);
  318. LUACHECKOBJ(ele);
  319. Element* child = ele->GetLastChild();
  320. if(child == NULL)
  321. lua_pushnil(L);
  322. else
  323. LuaType<Element>::push(L,child,false);
  324. return 1;
  325. }
  326. int ElementGetAttrnext_sibling(lua_State* L)
  327. {
  328. Element* ele = LuaType<Element>::check(L,1);
  329. LUACHECKOBJ(ele);
  330. Element* sibling = ele->GetNextSibling();
  331. if(sibling == NULL)
  332. lua_pushnil(L);
  333. else
  334. LuaType<Element>::push(L,sibling,false);
  335. return 1;
  336. }
  337. int ElementGetAttroffset_height(lua_State* L)
  338. {
  339. Element* ele = LuaType<Element>::check(L,1);
  340. LUACHECKOBJ(ele);
  341. lua_pushnumber(L,ele->GetOffsetHeight());
  342. return 1;
  343. }
  344. int ElementGetAttroffset_left(lua_State* L)
  345. {
  346. Element* ele = LuaType<Element>::check(L,1);
  347. LUACHECKOBJ(ele);
  348. lua_pushnumber(L,ele->GetOffsetLeft());
  349. return 1;
  350. }
  351. int ElementGetAttroffset_parent(lua_State* L)
  352. {
  353. Element* ele = LuaType<Element>::check(L,1);
  354. LUACHECKOBJ(ele);
  355. Element* parent = ele->GetOffsetParent();
  356. LuaType<Element>::push(L,parent,false);
  357. return 1;
  358. }
  359. int ElementGetAttroffset_top(lua_State* L)
  360. {
  361. Element* ele = LuaType<Element>::check(L,1);
  362. LUACHECKOBJ(ele);
  363. lua_pushnumber(L, ele->GetOffsetTop());
  364. return 1;
  365. }
  366. int ElementGetAttroffset_width(lua_State* L)
  367. {
  368. Element* ele = LuaType<Element>::check(L,1);
  369. LUACHECKOBJ(ele);
  370. lua_pushnumber(L,ele->GetOffsetWidth());
  371. return 1;
  372. }
  373. int ElementGetAttrowner_document(lua_State* L)
  374. {
  375. Element* ele = LuaType<Element>::check(L,1);
  376. LUACHECKOBJ(ele);
  377. Document* doc = ele->GetOwnerDocument();
  378. LuaType<Document>::push(L,doc,false);
  379. return 1;
  380. }
  381. int ElementGetAttrparent_node(lua_State* L)
  382. {
  383. Element* ele = LuaType<Element>::check(L,1);
  384. Element* parent = ele->GetParentNode();
  385. if(parent == NULL)
  386. lua_pushnil(L);
  387. else
  388. LuaType<Element>::push(L,parent,false);
  389. return 1;
  390. }
  391. int ElementGetAttrprevious_sibling(lua_State* L)
  392. {
  393. Element* ele = LuaType<Element>::check(L,1);
  394. Element* sibling = ele->GetPreviousSibling();
  395. if(sibling == NULL)
  396. lua_pushnil(L);
  397. else
  398. LuaType<Element>::push(L,sibling,false);
  399. return 1;
  400. }
  401. int ElementGetAttrscroll_height(lua_State* L)
  402. {
  403. Element* ele = LuaType<Element>::check(L,1);
  404. lua_pushnumber(L,ele->GetScrollHeight());
  405. return 1;
  406. }
  407. int ElementGetAttrscroll_left(lua_State* L)
  408. {
  409. Element* ele = LuaType<Element>::check(L,1);
  410. lua_pushnumber(L,ele->GetScrollLeft());
  411. return 1;
  412. }
  413. int ElementGetAttrscroll_top(lua_State* L)
  414. {
  415. Element* ele = LuaType<Element>::check(L,1);
  416. lua_pushnumber(L,ele->GetScrollTop());
  417. return 1;
  418. }
  419. int ElementGetAttrscroll_width(lua_State* L)
  420. {
  421. Element* ele = LuaType<Element>::check(L,1);
  422. lua_pushnumber(L,ele->GetScrollWidth());
  423. return 1;
  424. }
  425. int ElementGetAttrstyle(lua_State* L)
  426. {
  427. Element* ele = LuaType<Element>::check(L,1);
  428. LuaType<ElementStyle>::push(L,ele->GetStyle(),false);
  429. return 1;
  430. }
  431. int ElementGetAttrtag_name(lua_State* L)
  432. {
  433. Element* ele = LuaType<Element>::check(L,1);
  434. lua_pushstring(L,ele->GetTagName().CString());
  435. return 0;
  436. }
  437. //setters
  438. int ElementSetAttrclass_name(lua_State* L)
  439. {
  440. Element* ele = LuaType<Element>::check(L,1);
  441. const char* name = luaL_checkstring(L,2);
  442. ele->SetClassNames(name);
  443. return 0;
  444. }
  445. int ElementSetAttrid(lua_State* L)
  446. {
  447. Element* ele = LuaType<Element>::check(L,1);
  448. const char* id = luaL_checkstring(L,2);
  449. ele->SetId(id);
  450. return 0;
  451. }
  452. int ElementSetAttrinner_rml(lua_State* L)
  453. {
  454. Element* ele = LuaType<Element>::check(L,1);
  455. const char* rml = luaL_checkstring(L,2);
  456. ele->SetInnerRML(rml);
  457. return 0;
  458. }
  459. int ElementSetAttrscroll_left(lua_State* L)
  460. {
  461. Element* ele = LuaType<Element>::check(L,1);
  462. float scroll = (float)luaL_checknumber(L,2);
  463. ele->SetScrollLeft(scroll);
  464. return 0;
  465. }
  466. int ElementSetAttrscroll_top(lua_State* L)
  467. {
  468. Element* ele = LuaType<Element>::check(L,1);
  469. float scroll = (float)luaL_checknumber(L,2);
  470. ele->SetScrollTop(scroll);
  471. return 0;
  472. }
  473. RegType<Element> ElementMethods[] =
  474. {
  475. LUAMETHOD(Element,AddEventListener)
  476. LUAMETHOD(Element,AppendChild)
  477. LUAMETHOD(Element,Blur)
  478. LUAMETHOD(Element,Click)
  479. LUAMETHOD(Element,DispatchEvent)
  480. LUAMETHOD(Element,Focus)
  481. LUAMETHOD(Element,GetAttribute)
  482. LUAMETHOD(Element,GetElementById)
  483. LUAMETHOD(Element,GetElementsByTagName)
  484. LUAMETHOD(Element,HasAttribute)
  485. LUAMETHOD(Element,HasChildNodes)
  486. LUAMETHOD(Element,InsertBefore)
  487. LUAMETHOD(Element,IsClassSet)
  488. LUAMETHOD(Element,RemoveAttribute)
  489. LUAMETHOD(Element,RemoveChild)
  490. LUAMETHOD(Element,ReplaceChild)
  491. LUAMETHOD(Element,ScrollIntoView)
  492. LUAMETHOD(Element,SetAttribute)
  493. LUAMETHOD(Element,SetClass)
  494. { NULL, NULL },
  495. };
  496. luaL_reg ElementGetters[] =
  497. {
  498. LUAGETTER(Element,attributes)
  499. LUAGETTER(Element,child_nodes)
  500. LUAGETTER(Element,class_name)
  501. LUAGETTER(Element,client_left)
  502. LUAGETTER(Element,client_height)
  503. LUAGETTER(Element,client_top)
  504. LUAGETTER(Element,client_width)
  505. LUAGETTER(Element,first_child)
  506. LUAGETTER(Element,id)
  507. LUAGETTER(Element,inner_rml)
  508. LUAGETTER(Element,last_child)
  509. LUAGETTER(Element,next_sibling)
  510. LUAGETTER(Element,offset_height)
  511. LUAGETTER(Element,offset_left)
  512. LUAGETTER(Element,offset_parent)
  513. LUAGETTER(Element,offset_top)
  514. LUAGETTER(Element,offset_width)
  515. LUAGETTER(Element,owner_document)
  516. LUAGETTER(Element,parent_node)
  517. LUAGETTER(Element,previous_sibling)
  518. LUAGETTER(Element,scroll_height)
  519. LUAGETTER(Element,scroll_left)
  520. LUAGETTER(Element,scroll_top)
  521. LUAGETTER(Element,scroll_width)
  522. LUAGETTER(Element,style)
  523. LUAGETTER(Element,tag_name)
  524. { NULL, NULL },
  525. };
  526. luaL_reg ElementSetters[] =
  527. {
  528. LUASETTER(Element,class_name)
  529. LUASETTER(Element,id)
  530. LUASETTER(Element,inner_rml)
  531. LUASETTER(Element,scroll_left)
  532. LUASETTER(Element,scroll_top)
  533. { NULL, NULL },
  534. };
  535. LUATYPEDEFINE(Element,true)
  536. }
  537. }
  538. }