ElementStyle.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. int ElementStyle__index(lua_State* L)
  36. {
  37. /*the table obj and the missing key are currently on the stack(index 1 & 2) as defined by the Lua language*/
  38. int keytype = lua_type(L,2);
  39. if(keytype == LUA_TSTRING) //if we are trying to access a string, then we will assume that it is a property
  40. {
  41. ElementStyle* es = LuaType<ElementStyle>::check(L,1);
  42. if(es == NULL)
  43. {
  44. lua_pushnil(L);
  45. return 1;
  46. }
  47. const Property* prop = es->GetProperty(lua_tostring(L,2));
  48. if(prop == NULL)
  49. {
  50. lua_pushnil(L);
  51. return 1;
  52. }
  53. else
  54. {
  55. lua_pushstring(L,prop->ToString().CString());
  56. return 1;
  57. }
  58. }
  59. else //if it wasn't trying to get a string
  60. {
  61. lua_settop(L,2);
  62. return LuaType<ElementStyle>::index(L);
  63. }
  64. }
  65. int ElementStyle__newindex(lua_State* L)
  66. {
  67. //[1] = obj, [2] = key, [3] = value
  68. ElementStyle* es = LuaType<ElementStyle>::check(L,1);
  69. if(es == NULL)
  70. {
  71. lua_pushnil(L);
  72. return 1;
  73. }
  74. int keytype = lua_type(L,2);
  75. int valuetype = lua_type(L,3);
  76. if(keytype == LUA_TSTRING )
  77. {
  78. const char* key = lua_tostring(L,2);
  79. if(valuetype == LUA_TSTRING)
  80. {
  81. const char* value = lua_tostring(L,3);
  82. lua_pushboolean(L,es->SetProperty(key,value));
  83. return 1;
  84. }
  85. else if (valuetype == LUA_TNIL)
  86. {
  87. es->RemoveProperty(key);
  88. return 0;
  89. }
  90. }
  91. //everything else returns when it needs to, so we are safe to pass it
  92. //on if needed
  93. lua_settop(L,3);
  94. return LuaType<ElementStyle>::newindex(L);
  95. }
  96. int ElementStyleGetTable(lua_State* L, ElementStyle* obj)
  97. {
  98. int index = 0;
  99. String key,sval;
  100. const Property* value;
  101. PseudoClassList pseudo;
  102. lua_newtable(L);
  103. while(obj->IterateProperties(index,pseudo,key,value))
  104. {
  105. lua_pushstring(L,key.CString());
  106. value->definition->GetValue(sval,*value);
  107. lua_pushstring(L,sval.CString());
  108. lua_settable(L,-3);
  109. }
  110. return 1;
  111. }
  112. RegType<ElementStyle> ElementStyleMethods[] =
  113. {
  114. LUAMETHOD(ElementStyle,GetTable)
  115. { NULL, NULL },
  116. };
  117. luaL_reg ElementStyleGetters[] =
  118. {
  119. { NULL, NULL },
  120. };
  121. luaL_reg ElementStyleSetters[] =
  122. {
  123. { NULL, NULL },
  124. };
  125. /*
  126. template<> const char* GetTClassName<ElementStyle>() { return "ElementStyle"; }
  127. template<> RegType<ElementStyle>* GetMethodTable<ElementStyle>() { return ElementStyleMethods; }
  128. template<> luaL_reg* GetAttrTable<ElementStyle>() { return ElementStyleGetters; }
  129. template<> luaL_reg* SetAttrTable<ElementStyle>() { return ElementStyleSetters; }
  130. */
  131. }
  132. }
  133. }