ElementFormControlSelect.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #include "precompiled.h"
  2. #include "ElementFormControlSelect.h"
  3. #include <Rocket/Controls/ElementFormControlSelect.h>
  4. #include <Rocket/Controls/ElementFormControl.h>
  5. #include <Rocket/Core/Element.h>
  6. using Rocket::Controls::ElementFormControlSelect;
  7. using Rocket::Controls::ElementFormControl;
  8. namespace Rocket {
  9. namespace Core {
  10. namespace Lua {
  11. //inherits from ElementFormControl which inherits from Element
  12. template<> void LuaType<ElementFormControlSelect>::extra_init(lua_State* L, int metatable_index)
  13. {
  14. //init whatever elementformcontrol did extra, like inheritance
  15. LuaType<ElementFormControl>::extra_init(L,metatable_index);
  16. //then inherit from elementformcontrol
  17. LuaType<ElementFormControl>::_regfunctions(L,metatable_index,metatable_index-1);
  18. }
  19. //methods
  20. int ElementFormControlSelectAdd(lua_State* L, ElementFormControlSelect* obj)
  21. {
  22. LUACHECKOBJ(obj);
  23. const char* rml = luaL_checkstring(L,1);
  24. const char* value = luaL_checkstring(L,2);
  25. int before = -1; //default
  26. if(lua_gettop(L) >= 3)
  27. before = luaL_checkint(L,3);
  28. int index = obj->Add(rml,value,before);
  29. lua_pushinteger(L,index);
  30. return 1;
  31. }
  32. int ElementFormControlSelectRemove(lua_State* L, ElementFormControlSelect* obj)
  33. {
  34. LUACHECKOBJ(obj);
  35. int index = luaL_checkint(L,1);
  36. obj->Remove(index);
  37. return 0;
  38. }
  39. int ElementFormControlSelectGetOption(lua_State* L, ElementFormControlSelect* obj)
  40. {
  41. LUACHECKOBJ(obj);
  42. int index = luaL_checkint(L,1);
  43. Rocket::Controls::SelectOption* opt = obj->GetOption(index);
  44. lua_newtable(L);
  45. LuaType<Element>::push(L,opt->GetElement(),false);
  46. lua_setfield(L,-2,"element");
  47. lua_pushstring(L,opt->GetValue().CString());
  48. lua_setfield(L,-2,"value");
  49. return 1;
  50. }
  51. //getters
  52. int ElementFormControlSelectGetAttroptions(lua_State* L)
  53. {
  54. ElementFormControlSelect* obj = LuaType<ElementFormControlSelect>::check(L,1);
  55. LUACHECKOBJ(obj);
  56. int numOptions = obj->GetNumOptions();
  57. //local variables for the loop
  58. Rocket::Controls::SelectOption* opt;
  59. Element* ele;
  60. String value;
  61. lua_newtable(L);
  62. int retindex = lua_gettop(L);
  63. for(int index = 0; index < numOptions; index++)
  64. {
  65. opt = obj->GetOption(index);
  66. if(opt == NULL) continue;
  67. ele = opt->GetElement();
  68. value = opt->GetValue();
  69. lua_newtable(L);
  70. LuaType<Element>::push(L,ele,false);
  71. lua_setfield(L,-2,"element");
  72. lua_pushstring(L,value.CString());
  73. lua_setfield(L,-2,"value");
  74. lua_rawseti(L,retindex,index); //sets the table that is being returned's 'index' to be the table with element and value
  75. lua_pop(L,1); //pops the table with element,value from the stack
  76. }
  77. return 1;
  78. }
  79. int ElementFormControlSelectGetAttrselection(lua_State* L)
  80. {
  81. ElementFormControlSelect* obj = LuaType<ElementFormControlSelect>::check(L,1);
  82. LUACHECKOBJ(obj);
  83. int selection = obj->GetSelection();
  84. lua_pushinteger(L,selection);
  85. return 1;
  86. }
  87. //setter
  88. int ElementFormControlSelectSetAttrselection(lua_State* L)
  89. {
  90. ElementFormControlSelect* obj = LuaType<ElementFormControlSelect>::check(L,1);
  91. LUACHECKOBJ(obj);
  92. int selection = luaL_checkint(L,2);
  93. obj->SetSelection(selection);
  94. return 0;
  95. }
  96. RegType<ElementFormControlSelect> ElementFormControlSelectMethods[] =
  97. {
  98. LUAMETHOD(ElementFormControlSelect,Add)
  99. LUAMETHOD(ElementFormControlSelect,Remove)
  100. LUAMETHOD(ElementFormControlSelect,GetOption)
  101. { NULL, NULL },
  102. };
  103. luaL_reg ElementFormControlSelectGetters[] =
  104. {
  105. LUAGETTER(ElementFormControlSelect,options)
  106. LUAGETTER(ElementFormControlSelect,selection)
  107. { NULL, NULL },
  108. };
  109. luaL_reg ElementFormControlSelectSetters[] =
  110. {
  111. LUASETTER(ElementFormControlSelect,selection)
  112. { NULL, NULL },
  113. };
  114. template<> const char* GetTClassName<ElementFormControlSelect>() { return "ElementFormControlSelect"; }
  115. template<> RegType<ElementFormControlSelect>* GetMethodTable<ElementFormControlSelect>() { return ElementFormControlSelectMethods; }
  116. template<> luaL_reg* GetAttrTable<ElementFormControlSelect>() { return ElementFormControlSelectGetters; }
  117. template<> luaL_reg* SetAttrTable<ElementFormControlSelect>() { return ElementFormControlSelectSetters; }
  118. }
  119. }
  120. }