ElementFormControlSelect.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include "precompiled.h"
  2. #include "ElementFormControlSelect.h"
  3. #include <Rocket/Controls/ElementFormControlSelect.h>
  4. #include <Rocket/Controls/ElementFormControl.h>
  5. #include <Rocket/Core/Element.h>
  6. using Rocket::Controls::ElementFormControlSelect;
  7. using Rocket::Controls::ElementFormControl;
  8. namespace Rocket {
  9. namespace Core {
  10. namespace Lua {
  11. //inherits from ElementFormControl which inherits from Element
  12. //methods
  13. int ElementFormControlSelectAdd(lua_State* L, ElementFormControlSelect* obj)
  14. {
  15. LUACHECKOBJ(obj);
  16. const char* rml = luaL_checkstring(L,1);
  17. const char* value = luaL_checkstring(L,2);
  18. int before = -1; //default
  19. if(lua_gettop(L) >= 3)
  20. before = luaL_checkint(L,3);
  21. int index = obj->Add(rml,value,before);
  22. lua_pushinteger(L,index);
  23. return 1;
  24. }
  25. int ElementFormControlSelectRemove(lua_State* L, ElementFormControlSelect* obj)
  26. {
  27. LUACHECKOBJ(obj);
  28. int index = luaL_checkint(L,1);
  29. obj->Remove(index);
  30. return 0;
  31. }
  32. int ElementFormControlSelectGetOption(lua_State* L, ElementFormControlSelect* obj)
  33. {
  34. LUACHECKOBJ(obj);
  35. int index = luaL_checkint(L,1);
  36. Rocket::Controls::SelectOption* opt = obj->GetOption(index);
  37. lua_newtable(L);
  38. LuaType<Element>::push(L,opt->GetElement(),false);
  39. lua_setfield(L,-2,"element");
  40. lua_pushstring(L,opt->GetValue().CString());
  41. lua_setfield(L,-2,"value");
  42. return 1;
  43. }
  44. //getters
  45. int ElementFormControlSelectGetAttroptions(lua_State* L)
  46. {
  47. ElementFormControlSelect* obj = LuaType<ElementFormControlSelect>::check(L,1);
  48. LUACHECKOBJ(obj);
  49. int numOptions = obj->GetNumOptions();
  50. //local variables for the loop
  51. Rocket::Controls::SelectOption* opt;
  52. Element* ele;
  53. String value;
  54. lua_newtable(L);
  55. int retindex = lua_gettop(L);
  56. for(int index = 0; index < numOptions; index++)
  57. {
  58. opt = obj->GetOption(index);
  59. if(opt == NULL) continue;
  60. ele = opt->GetElement();
  61. value = opt->GetValue();
  62. lua_newtable(L);
  63. LuaType<Element>::push(L,ele,false);
  64. lua_setfield(L,-2,"element");
  65. lua_pushstring(L,value.CString());
  66. lua_setfield(L,-2,"value");
  67. lua_rawseti(L,retindex,index); //sets the table that is being returned's 'index' to be the table with element and value
  68. lua_pop(L,1); //pops the table with element,value from the stack
  69. }
  70. return 1;
  71. }
  72. int ElementFormControlSelectGetAttrselection(lua_State* L)
  73. {
  74. ElementFormControlSelect* obj = LuaType<ElementFormControlSelect>::check(L,1);
  75. LUACHECKOBJ(obj);
  76. int selection = obj->GetSelection();
  77. lua_pushinteger(L,selection);
  78. return 1;
  79. }
  80. //setter
  81. int ElementFormControlSelectSetAttrselection(lua_State* L)
  82. {
  83. ElementFormControlSelect* obj = LuaType<ElementFormControlSelect>::check(L,1);
  84. LUACHECKOBJ(obj);
  85. int selection = luaL_checkint(L,2);
  86. obj->SetSelection(selection);
  87. return 0;
  88. }
  89. RegType<ElementFormControlSelect> ElementFormControlSelectMethods[] =
  90. {
  91. LUAMETHOD(ElementFormControlSelect,Add)
  92. LUAMETHOD(ElementFormControlSelect,Remove)
  93. LUAMETHOD(ElementFormControlSelect,GetOption)
  94. { NULL, NULL },
  95. };
  96. luaL_reg ElementFormControlSelectGetters[] =
  97. {
  98. LUAGETTER(ElementFormControlSelect,options)
  99. LUAGETTER(ElementFormControlSelect,selection)
  100. { NULL, NULL },
  101. };
  102. luaL_reg ElementFormControlSelectSetters[] =
  103. {
  104. LUASETTER(ElementFormControlSelect,selection)
  105. { NULL, NULL },
  106. };
  107. /*
  108. template<> const char* GetTClassName<ElementFormControlSelect>() { return "ElementFormControlSelect"; }
  109. template<> RegType<ElementFormControlSelect>* GetMethodTable<ElementFormControlSelect>() { return ElementFormControlSelectMethods; }
  110. template<> luaL_reg* GetAttrTable<ElementFormControlSelect>() { return ElementFormControlSelectGetters; }
  111. template<> luaL_reg* SetAttrTable<ElementFormControlSelect>() { return ElementFormControlSelectSetters; }
  112. */
  113. }
  114. }
  115. }