2
0

ElementFormControlSelect.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include "ElementFormControlSelect.h"
  2. #include "ElementFormControl.h"
  3. #include "SelectOptionsProxy.h"
  4. #include <RmlUi/Core/Element.h>
  5. #include <RmlUi/Core/Elements/ElementFormControl.h>
  6. #include <RmlUi/Core/Elements/ElementFormControlSelect.h>
  7. #include <RmlUi/Lua/Utilities.h>
  8. namespace Rml {
  9. namespace Lua {
  10. // methods
  11. int ElementFormControlSelectAdd(lua_State* L, ElementFormControlSelect* obj)
  12. {
  13. const char* rml = luaL_checkstring(L, 1);
  14. const char* value = luaL_checkstring(L, 2);
  15. int before = -1; // default
  16. if (lua_gettop(L) >= 3)
  17. before = GetIndex(L, 3);
  18. int index = obj->Add(rml, value, before);
  19. lua_pushinteger(L, index);
  20. return 1;
  21. }
  22. int ElementFormControlSelectRemove(lua_State* L, ElementFormControlSelect* obj)
  23. {
  24. int index = GetIndex(L, 1);
  25. obj->Remove(index);
  26. return 0;
  27. }
  28. int ElementFormControlSelectRemoveAll(lua_State* /*L*/, ElementFormControlSelect* obj)
  29. {
  30. obj->RemoveAll();
  31. return 0;
  32. }
  33. // getters
  34. int ElementFormControlSelectGetAttroptions(lua_State* L)
  35. {
  36. ElementFormControlSelect* obj = LuaType<ElementFormControlSelect>::check(L, 1);
  37. RMLUI_CHECK_OBJ(obj);
  38. SelectOptionsProxy* proxy = new SelectOptionsProxy();
  39. proxy->owner = obj;
  40. LuaType<SelectOptionsProxy>::push(L, proxy, true);
  41. return 1;
  42. }
  43. int ElementFormControlSelectGetAttrselection(lua_State* L)
  44. {
  45. ElementFormControlSelect* obj = LuaType<ElementFormControlSelect>::check(L, 1);
  46. RMLUI_CHECK_OBJ(obj);
  47. int selection = obj->GetSelection();
  48. PushIndex(L, selection);
  49. return 1;
  50. }
  51. // setter
  52. int ElementFormControlSelectSetAttrselection(lua_State* L)
  53. {
  54. ElementFormControlSelect* obj = LuaType<ElementFormControlSelect>::check(L, 1);
  55. RMLUI_CHECK_OBJ(obj);
  56. int selection = GetIndex(L, 2);
  57. obj->SetSelection(selection);
  58. return 0;
  59. }
  60. RegType<ElementFormControlSelect> ElementFormControlSelectMethods[] = {
  61. RMLUI_LUAMETHOD(ElementFormControlSelect, Add),
  62. RMLUI_LUAMETHOD(ElementFormControlSelect, Remove),
  63. RMLUI_LUAMETHOD(ElementFormControlSelect, RemoveAll),
  64. {nullptr, nullptr},
  65. };
  66. luaL_Reg ElementFormControlSelectGetters[] = {
  67. RMLUI_LUAGETTER(ElementFormControlSelect, options),
  68. RMLUI_LUAGETTER(ElementFormControlSelect, selection),
  69. {nullptr, nullptr},
  70. };
  71. luaL_Reg ElementFormControlSelectSetters[] = {
  72. RMLUI_LUASETTER(ElementFormControlSelect, selection),
  73. {nullptr, nullptr},
  74. };
  75. // inherits from ElementFormControl which inherits from Element
  76. template <>
  77. void ExtraInit<ElementFormControlSelect>(lua_State* L, int metatable_index)
  78. {
  79. // init whatever elementformcontrol did extra, like inheritance
  80. ExtraInit<ElementFormControl>(L, metatable_index);
  81. // then inherit from elementformcontrol
  82. LuaType<ElementFormControl>::_regfunctions(L, metatable_index, metatable_index - 1);
  83. AddTypeToElementAsTable<ElementFormControlSelect>(L);
  84. }
  85. RMLUI_LUATYPE_DEFINE(ElementFormControlSelect)
  86. } // namespace Lua
  87. } // namespace Rml