ElementFormControlSelect.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 "precompiled.h"
  29. #include "ElementFormControlSelect.h"
  30. #include "SelectOptionsProxy.h"
  31. #include <RmlUi/Controls/ElementFormControlSelect.h>
  32. #include <RmlUi/Controls/ElementFormControl.h>
  33. #include <RmlUi/Core/Element.h>
  34. #include "ElementFormControl.h"
  35. #include <RmlUi/Core/Lua/Utilities.h>
  36. namespace Rml {
  37. namespace Controls {
  38. namespace Lua {
  39. //methods
  40. int ElementFormControlSelectAdd(lua_State* L, ElementFormControlSelect* obj)
  41. {
  42. const char* rml = luaL_checkstring(L,1);
  43. const char* value = luaL_checkstring(L,2);
  44. int before = -1; //default
  45. if(lua_gettop(L) >= 3)
  46. before = (int)luaL_checkinteger(L,3);
  47. int index = obj->Add(rml,value,before);
  48. lua_pushinteger(L,index);
  49. return 1;
  50. }
  51. int ElementFormControlSelectRemove(lua_State* L, ElementFormControlSelect* obj)
  52. {
  53. int index = (int)luaL_checkinteger(L,1);
  54. obj->Remove(index);
  55. return 0;
  56. }
  57. int ElementFormControlSelectRemoveAll(lua_State* /*L*/, ElementFormControlSelect* obj)
  58. {
  59. obj->RemoveAll();
  60. return 0;
  61. }
  62. //getters
  63. int ElementFormControlSelectGetAttroptions(lua_State* L)
  64. {
  65. ElementFormControlSelect* obj = LuaType<ElementFormControlSelect>::check(L,1);
  66. LUACHECKOBJ(obj);
  67. SelectOptionsProxy* proxy = new SelectOptionsProxy();
  68. proxy->owner = obj;
  69. LuaType<SelectOptionsProxy>::push(L,proxy,true);
  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 = (int)luaL_checkinteger(L,2);
  86. obj->SetSelection(selection);
  87. return 0;
  88. }
  89. Rml::Core::Lua::RegType<ElementFormControlSelect> ElementFormControlSelectMethods[] =
  90. {
  91. LUAMETHOD(ElementFormControlSelect,Add)
  92. LUAMETHOD(ElementFormControlSelect,Remove)
  93. LUAMETHOD(ElementFormControlSelect,RemoveAll)
  94. { nullptr, nullptr },
  95. };
  96. luaL_Reg ElementFormControlSelectGetters[] =
  97. {
  98. LUAGETTER(ElementFormControlSelect,options)
  99. LUAGETTER(ElementFormControlSelect,selection)
  100. { nullptr, nullptr },
  101. };
  102. luaL_Reg ElementFormControlSelectSetters[] =
  103. {
  104. LUASETTER(ElementFormControlSelect,selection)
  105. { nullptr, nullptr },
  106. };
  107. }
  108. }
  109. }
  110. namespace Rml {
  111. namespace Core {
  112. namespace Lua {
  113. //inherits from ElementFormControl which inherits from Element
  114. template<> void ExtraInit<Rml::Controls::ElementFormControlSelect>(lua_State* L, int metatable_index)
  115. {
  116. //init whatever elementformcontrol did extra, like inheritance
  117. ExtraInit<Rml::Controls::ElementFormControl>(L,metatable_index);
  118. //then inherit from elementformcontrol
  119. LuaType<Rml::Controls::ElementFormControl>::_regfunctions(L,metatable_index,metatable_index-1);
  120. AddTypeToElementAsTable<Rml::Controls::ElementFormControlSelect>(L);
  121. }
  122. using Rml::Controls::ElementFormControlSelect;
  123. LUACONTROLSTYPEDEFINE(ElementFormControlSelect)
  124. }
  125. }
  126. }