ElementStyle.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 <Rocket/Core/Lua/LuaType.h>
  29. #include <Rocket/Core/Lua/lua.hpp>
  30. #include "ElementStyle.h"
  31. #include <ElementStyle.h>
  32. namespace Rocket {
  33. namespace Core {
  34. namespace Lua {
  35. template<> void ExtraInit<ElementStyle>(lua_State* L, int metatable_index)
  36. {
  37. lua_pushcfunction(L,ElementStyle__index);
  38. lua_setfield(L,metatable_index,"__index");
  39. lua_pushcfunction(L,ElementStyle__newindex);
  40. lua_setfield(L,metatable_index,"__newindex");
  41. lua_pushcfunction(L,ElementStyle__pairs);
  42. lua_setfield(L,metatable_index,"__pairs");
  43. lua_pushcfunction(L,ElementStyle__ipairs);
  44. lua_setfield(L,metatable_index,"__ipairs");
  45. }
  46. int ElementStyle__index(lua_State* L)
  47. {
  48. /*the table obj and the missing key are currently on the stack(index 1 & 2) as defined by the Lua language*/
  49. int keytype = lua_type(L,2);
  50. if(keytype == LUA_TSTRING) //if we are trying to access a string, then we will assume that it is a property
  51. {
  52. ElementStyle* es = LuaType<ElementStyle>::check(L,1);
  53. if(es == NULL)
  54. {
  55. lua_pushnil(L);
  56. return 1;
  57. }
  58. const Property* prop = es->GetProperty(lua_tostring(L,2));
  59. if(prop == NULL)
  60. {
  61. lua_pushnil(L);
  62. return 1;
  63. }
  64. else
  65. {
  66. lua_pushstring(L,prop->ToString().CString());
  67. return 1;
  68. }
  69. }
  70. else //if it wasn't trying to get a string
  71. {
  72. lua_settop(L,2);
  73. return LuaType<ElementStyle>::index(L);
  74. }
  75. }
  76. int ElementStyle__newindex(lua_State* L)
  77. {
  78. //[1] = obj, [2] = key, [3] = value
  79. ElementStyle* es = LuaType<ElementStyle>::check(L,1);
  80. if(es == NULL)
  81. {
  82. lua_pushnil(L);
  83. return 1;
  84. }
  85. int keytype = lua_type(L,2);
  86. int valuetype = lua_type(L,3);
  87. if(keytype == LUA_TSTRING )
  88. {
  89. const char* key = lua_tostring(L,2);
  90. if(valuetype == LUA_TSTRING)
  91. {
  92. const char* value = lua_tostring(L,3);
  93. lua_pushboolean(L,es->SetProperty(key,value));
  94. return 1;
  95. }
  96. else if (valuetype == LUA_TNIL)
  97. {
  98. es->RemoveProperty(key);
  99. return 0;
  100. }
  101. }
  102. //everything else returns when it needs to, so we are safe to pass it
  103. //on if needed
  104. lua_settop(L,3);
  105. return LuaType<ElementStyle>::newindex(L);
  106. }
  107. //[1] is the object, [2] is the last used key, [3] is the userdata
  108. int ElementStyle__pairs(lua_State* L)
  109. {
  110. ElementStyle* obj = LuaType<ElementStyle>::check(L,1);
  111. LUACHECKOBJ(obj);
  112. int* pindex = (int*)lua_touserdata(L,3);
  113. if((*pindex) == -1)
  114. *pindex = 0;
  115. //iterate variables
  116. String key,val;
  117. const Property* prop;
  118. PseudoClassList pseudo;
  119. if(obj->IterateProperties((*pindex),pseudo,key,prop))
  120. {
  121. prop->definition->GetValue(val,*prop);
  122. lua_pushstring(L,key.CString());
  123. lua_pushstring(L,val.CString());
  124. }
  125. else
  126. {
  127. lua_pushnil(L);
  128. lua_pushnil(L);
  129. }
  130. return 2;
  131. }
  132. //only indexed by string
  133. int ElementStyle__ipairs(lua_State* L)
  134. {
  135. lua_pushnil(L);
  136. lua_pushnil(L);
  137. return 2;
  138. }
  139. RegType<ElementStyle> ElementStyleMethods[] =
  140. {
  141. { NULL, NULL },
  142. };
  143. luaL_reg ElementStyleGetters[] =
  144. {
  145. { NULL, NULL },
  146. };
  147. luaL_reg ElementStyleSetters[] =
  148. {
  149. { NULL, NULL },
  150. };
  151. LUATYPEDEFINE(ElementStyle,false)
  152. }
  153. }
  154. }