ElementChildNodesProxy.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 "ElementChildNodesProxy.h"
  29. #include "Element.h"
  30. namespace Rocket {
  31. namespace Core {
  32. namespace Lua {
  33. template<> void ExtraInit<ElementChildNodesProxy>(lua_State* L, int metatable_index)
  34. {
  35. lua_pushcfunction(L,ElementChildNodesProxy__index);
  36. lua_setfield(L,metatable_index,"__index");
  37. }
  38. int ElementChildNodesProxy__index(lua_State* L)
  39. {
  40. /*the table obj and the missing key are currently on the stack(index 1 & 2) as defined by the Lua language*/
  41. int keytype = lua_type(L,2);
  42. if(keytype == LUA_TNUMBER) //only valid key types
  43. {
  44. ElementChildNodesProxy* obj = LuaType<ElementChildNodesProxy>::check(L,1);
  45. LUACHECKOBJ(obj);
  46. int key = luaL_checkint(L,2);
  47. Element* child = obj->owner->GetChild(key);
  48. LuaType<Element>::push(L,child,false);
  49. return 1;
  50. }
  51. else
  52. return LuaType<ElementChildNodesProxy>::index(L);
  53. }
  54. //method
  55. int ElementChildNodesProxyGetTable(lua_State* L, ElementChildNodesProxy* obj)
  56. {
  57. Element* ele = obj->owner;
  58. if(!ele->HasChildNodes())
  59. lua_pushnil(L);
  60. else
  61. {
  62. lua_newtable(L);
  63. int index = 0;
  64. int num_of_children = ele->GetNumChildren();
  65. while(index < num_of_children)
  66. {
  67. lua_pushinteger(L,index);
  68. LuaType<Element>::push(L,ele->GetChild(index),false);
  69. lua_settable(L,-3);
  70. ++index;
  71. }
  72. }
  73. return 1;
  74. }
  75. RegType<ElementChildNodesProxy> ElementChildNodesProxyMethods[] =
  76. {
  77. LUAMETHOD(ElementChildNodesProxy,GetTable)
  78. { NULL, NULL },
  79. };
  80. luaL_reg ElementChildNodesProxyGetters[] =
  81. {
  82. { NULL, NULL },
  83. };
  84. luaL_reg ElementChildNodesProxySetters[] =
  85. {
  86. { NULL, NULL },
  87. };
  88. LUATYPEDEFINE(ElementChildNodesProxy,true)
  89. }
  90. }
  91. }