ContextDocumentsProxy.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 "ContextDocumentsProxy.h"
  29. #include <Rocket/Core/ElementDocument.h>
  30. namespace Rocket {
  31. namespace Core {
  32. namespace Lua {
  33. typedef Rocket::Core::ElementDocument Document;
  34. template<> void ExtraInit<ContextDocumentsProxy>(lua_State* L, int metatable_index)
  35. {
  36. lua_pushcfunction(L,ContextDocumentsProxy__index);
  37. lua_setfield(L,metatable_index,"__index");
  38. }
  39. int ContextDocumentsProxy__index(lua_State* L)
  40. {
  41. /*the table obj and the missing key are currently on the stack(index 1 & 2) as defined by the Lua language*/
  42. int type = lua_type(L,2);
  43. if(type == LUA_TNUMBER || type == LUA_TSTRING) //only valid key types
  44. {
  45. ContextDocumentsProxy* proxy = LuaType<ContextDocumentsProxy>::check(L,1);
  46. Document* ret = NULL;
  47. if(type == LUA_TSTRING)
  48. ret = proxy->owner->GetDocument(luaL_checkstring(L,2));
  49. else
  50. ret = proxy->owner->GetDocument(luaL_checkint(L,2));
  51. LuaType<Document>::push(L,ret,false);
  52. return 1;
  53. }
  54. else
  55. return LuaType<ContextDocumentsProxy>::index(L);
  56. }
  57. //method
  58. int ContextDocumentsProxyGetTable(lua_State* L, ContextDocumentsProxy* obj)
  59. {
  60. Context* cont = obj->owner;
  61. Element* root = cont->GetRootElement();
  62. lua_newtable(L);
  63. int tableindex = lua_gettop(L);
  64. for(int i = 0; i < root->GetNumChildren(); i++)
  65. {
  66. Document* doc = root->GetChild(i)->GetOwnerDocument();
  67. if(doc == NULL)
  68. continue;
  69. LuaType<Document>::push(L,doc);
  70. lua_pushvalue(L,-1); //put it on the stack twice, since we assign it to
  71. //both a string and integer index
  72. lua_setfield(L, tableindex,doc->GetId().CString());
  73. lua_rawseti(L,tableindex,i);
  74. }
  75. lua_settop(L,tableindex); //to make sure
  76. return 1;
  77. }
  78. RegType<ContextDocumentsProxy> ContextDocumentsProxyMethods[] =
  79. {
  80. LUAMETHOD(ContextDocumentsProxy,GetTable)
  81. { NULL, NULL },
  82. };
  83. luaL_reg ContextDocumentsProxyGetters[] =
  84. {
  85. { NULL, NULL },
  86. };
  87. luaL_reg ContextDocumentsProxySetters[] =
  88. {
  89. { NULL, NULL },
  90. };
  91. LUATYPEDEFINE(ContextDocumentsProxy,false)
  92. }
  93. }
  94. }