ElementFormControlSelect.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019-2023 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "ElementFormControlSelect.h"
  29. #include "ElementFormControl.h"
  30. #include "SelectOptionsProxy.h"
  31. #include <RmlUi/Core/Element.h>
  32. #include <RmlUi/Core/Elements/ElementFormControl.h>
  33. #include <RmlUi/Core/Elements/ElementFormControlSelect.h>
  34. #include <RmlUi/Lua/Utilities.h>
  35. namespace Rml {
  36. namespace Lua {
  37. // methods
  38. int ElementFormControlSelectAdd(lua_State* L, ElementFormControlSelect* obj)
  39. {
  40. const char* rml = luaL_checkstring(L, 1);
  41. const char* value = luaL_checkstring(L, 2);
  42. int before = -1; // default
  43. if (lua_gettop(L) >= 3)
  44. before = GetIndex(L, 3);
  45. int index = obj->Add(rml, value, before);
  46. lua_pushinteger(L, index);
  47. return 1;
  48. }
  49. int ElementFormControlSelectRemove(lua_State* L, ElementFormControlSelect* obj)
  50. {
  51. int index = GetIndex(L, 1);
  52. obj->Remove(index);
  53. return 0;
  54. }
  55. int ElementFormControlSelectRemoveAll(lua_State* /*L*/, ElementFormControlSelect* obj)
  56. {
  57. obj->RemoveAll();
  58. return 0;
  59. }
  60. // getters
  61. int ElementFormControlSelectGetAttroptions(lua_State* L)
  62. {
  63. ElementFormControlSelect* obj = LuaType<ElementFormControlSelect>::check(L, 1);
  64. RMLUI_CHECK_OBJ(obj);
  65. SelectOptionsProxy* proxy = new SelectOptionsProxy();
  66. proxy->owner = obj;
  67. LuaType<SelectOptionsProxy>::push(L, proxy, true);
  68. return 1;
  69. }
  70. int ElementFormControlSelectGetAttrselection(lua_State* L)
  71. {
  72. ElementFormControlSelect* obj = LuaType<ElementFormControlSelect>::check(L, 1);
  73. RMLUI_CHECK_OBJ(obj);
  74. int selection = obj->GetSelection();
  75. PushIndex(L, selection);
  76. return 1;
  77. }
  78. // setter
  79. int ElementFormControlSelectSetAttrselection(lua_State* L)
  80. {
  81. ElementFormControlSelect* obj = LuaType<ElementFormControlSelect>::check(L, 1);
  82. RMLUI_CHECK_OBJ(obj);
  83. int selection = GetIndex(L, 2);
  84. obj->SetSelection(selection);
  85. return 0;
  86. }
  87. RegType<ElementFormControlSelect> ElementFormControlSelectMethods[] = {
  88. RMLUI_LUAMETHOD(ElementFormControlSelect, Add),
  89. RMLUI_LUAMETHOD(ElementFormControlSelect, Remove),
  90. RMLUI_LUAMETHOD(ElementFormControlSelect, RemoveAll),
  91. {nullptr, nullptr},
  92. };
  93. luaL_Reg ElementFormControlSelectGetters[] = {
  94. RMLUI_LUAGETTER(ElementFormControlSelect, options),
  95. RMLUI_LUAGETTER(ElementFormControlSelect, selection),
  96. {nullptr, nullptr},
  97. };
  98. luaL_Reg ElementFormControlSelectSetters[] = {
  99. RMLUI_LUASETTER(ElementFormControlSelect, selection),
  100. {nullptr, nullptr},
  101. };
  102. // inherits from ElementFormControl which inherits from Element
  103. template <>
  104. void ExtraInit<ElementFormControlSelect>(lua_State* L, int metatable_index)
  105. {
  106. // init whatever elementformcontrol did extra, like inheritance
  107. ExtraInit<ElementFormControl>(L, metatable_index);
  108. // then inherit from elementformcontrol
  109. LuaType<ElementFormControl>::_regfunctions(L, metatable_index, metatable_index - 1);
  110. AddTypeToElementAsTable<ElementFormControlSelect>(L);
  111. }
  112. RMLUI_LUATYPE_DEFINE(ElementFormControlSelect)
  113. } // namespace Lua
  114. } // namespace Rml