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