ElementFormControlSelect.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 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 "SelectOptionsProxy.h"
  30. #include <RmlUi/Core/Elements/ElementFormControlSelect.h>
  31. #include <RmlUi/Core/Elements/ElementFormControl.h>
  32. #include <RmlUi/Core/Element.h>
  33. #include "ElementFormControl.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. {
  89. RMLUI_LUAMETHOD(ElementFormControlSelect,Add)
  90. RMLUI_LUAMETHOD(ElementFormControlSelect,Remove)
  91. RMLUI_LUAMETHOD(ElementFormControlSelect,RemoveAll)
  92. { nullptr, nullptr },
  93. };
  94. luaL_Reg ElementFormControlSelectGetters[] =
  95. {
  96. RMLUI_LUAGETTER(ElementFormControlSelect,options)
  97. RMLUI_LUAGETTER(ElementFormControlSelect,selection)
  98. { nullptr, nullptr },
  99. };
  100. luaL_Reg ElementFormControlSelectSetters[] =
  101. {
  102. RMLUI_LUASETTER(ElementFormControlSelect,selection)
  103. { nullptr, nullptr },
  104. };
  105. //inherits from ElementFormControl which inherits from Element
  106. template<> void ExtraInit<ElementFormControlSelect>(lua_State* L, int metatable_index)
  107. {
  108. //init whatever elementformcontrol did extra, like inheritance
  109. ExtraInit<ElementFormControl>(L,metatable_index);
  110. //then inherit from elementformcontrol
  111. LuaType<ElementFormControl>::_regfunctions(L,metatable_index,metatable_index-1);
  112. AddTypeToElementAsTable<ElementFormControlSelect>(L);
  113. }
  114. RMLUI_LUATYPE_DEFINE(ElementFormControlSelect)
  115. } // namespace Lua
  116. } // namespace Rml