ElementFormControlSelect.cpp 4.4 KB

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