Document.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #include "Document.h"
  2. #include "Element.h"
  3. #include <RmlUi/Core/Context.h>
  4. #include <RmlUi/Core/ElementDocument.h>
  5. #include <RmlUi/Lua/Utilities.h>
  6. namespace Rml {
  7. namespace Lua {
  8. template <>
  9. void ExtraInit<Document>(lua_State* L, int metatable_index)
  10. {
  11. // we will inherit from Element
  12. ExtraInit<Element>(L, metatable_index);
  13. LuaType<Element>::_regfunctions(L, metatable_index, metatable_index - 1);
  14. AddTypeToElementAsTable<Document>(L);
  15. // create the DocumentFocus table
  16. lua_getglobal(L, "DocumentModal");
  17. if (lua_isnoneornil(L, -1))
  18. {
  19. lua_pop(L, 1); // pop unsucessful getglobal
  20. lua_newtable(L); // create a table for holding the enum
  21. lua_pushinteger(L, (int)ModalFlag::None);
  22. lua_setfield(L, -2, "None");
  23. lua_pushinteger(L, (int)ModalFlag::Modal);
  24. lua_setfield(L, -2, "Modal");
  25. lua_pushinteger(L, (int)ModalFlag::Keep);
  26. lua_setfield(L, -2, "Keep");
  27. lua_setglobal(L, "DocumentModal");
  28. }
  29. // create the DocumentFocus table
  30. lua_getglobal(L, "DocumentFocus");
  31. if (lua_isnoneornil(L, -1))
  32. {
  33. lua_pop(L, 1); // pop unsucessful getglobal
  34. lua_newtable(L); // create a table for holding the enum
  35. lua_pushinteger(L, (int)FocusFlag::None);
  36. lua_setfield(L, -2, "None");
  37. lua_pushinteger(L, (int)FocusFlag::Document);
  38. lua_setfield(L, -2, "Document");
  39. lua_pushinteger(L, (int)FocusFlag::Keep);
  40. lua_setfield(L, -2, "Keep");
  41. lua_pushinteger(L, (int)FocusFlag::Auto);
  42. lua_setfield(L, -2, "Auto");
  43. lua_setglobal(L, "DocumentFocus");
  44. }
  45. }
  46. // methods
  47. int DocumentPullToFront(lua_State* /*L*/, Document* obj)
  48. {
  49. obj->PullToFront();
  50. return 0;
  51. }
  52. int DocumentPushToBack(lua_State* /*L*/, Document* obj)
  53. {
  54. obj->PushToBack();
  55. return 0;
  56. }
  57. int DocumentShow(lua_State* L, Document* obj)
  58. {
  59. int top = lua_gettop(L);
  60. if (top == 0)
  61. obj->Show();
  62. else if (top == 1)
  63. {
  64. ModalFlag modal = (ModalFlag)luaL_checkinteger(L, 1);
  65. obj->Show(modal);
  66. }
  67. else
  68. {
  69. ModalFlag modal = (ModalFlag)luaL_checkinteger(L, 1);
  70. FocusFlag focus = (FocusFlag)luaL_checkinteger(L, 2);
  71. obj->Show(modal, focus);
  72. }
  73. return 0;
  74. }
  75. int DocumentHide(lua_State* /*L*/, Document* obj)
  76. {
  77. obj->Hide();
  78. return 0;
  79. }
  80. int DocumentClose(lua_State* /*L*/, Document* obj)
  81. {
  82. obj->Close();
  83. return 0;
  84. }
  85. int DocumentCreateElement(lua_State* L, Document* obj)
  86. {
  87. const char* tag = luaL_checkstring(L, 1);
  88. ElementPtr* ele = new ElementPtr(obj->CreateElement(tag));
  89. LuaType<ElementPtr>::push(L, ele, true);
  90. return 1;
  91. }
  92. int DocumentCreateTextNode(lua_State* L, Document* obj)
  93. {
  94. // need ElementText object first
  95. const char* text = luaL_checkstring(L, 1);
  96. ElementPtr* et = new ElementPtr(obj->CreateTextNode(text));
  97. LuaType<ElementPtr>::push(L, et, true);
  98. return 1;
  99. }
  100. // getters
  101. int DocumentGetAttrtitle(lua_State* L)
  102. {
  103. Document* doc = LuaType<Document>::check(L, 1);
  104. RMLUI_CHECK_OBJ(doc);
  105. lua_pushstring(L, doc->GetTitle().c_str());
  106. return 1;
  107. }
  108. int DocumentGetAttrcontext(lua_State* L)
  109. {
  110. Document* doc = LuaType<Document>::check(L, 1);
  111. RMLUI_CHECK_OBJ(doc);
  112. LuaType<Context>::push(L, doc->GetContext(), false);
  113. return 1;
  114. }
  115. // setters
  116. int DocumentSetAttrtitle(lua_State* L)
  117. {
  118. Document* doc = LuaType<Document>::check(L, 1);
  119. RMLUI_CHECK_OBJ(doc);
  120. const char* title = luaL_checkstring(L, 2);
  121. doc->SetTitle(title);
  122. return 0;
  123. }
  124. RegType<Document> DocumentMethods[] = {
  125. RMLUI_LUAMETHOD(Document, PullToFront),
  126. RMLUI_LUAMETHOD(Document, PushToBack),
  127. RMLUI_LUAMETHOD(Document, Show),
  128. RMLUI_LUAMETHOD(Document, Hide),
  129. RMLUI_LUAMETHOD(Document, Close),
  130. RMLUI_LUAMETHOD(Document, CreateElement),
  131. RMLUI_LUAMETHOD(Document, CreateTextNode),
  132. {nullptr, nullptr},
  133. };
  134. luaL_Reg DocumentGetters[] = {
  135. RMLUI_LUAGETTER(Document, title),
  136. RMLUI_LUAGETTER(Document, context),
  137. {nullptr, nullptr},
  138. };
  139. luaL_Reg DocumentSetters[] = {
  140. RMLUI_LUASETTER(Document, title),
  141. {nullptr, nullptr},
  142. };
  143. RMLUI_LUATYPE_DEFINE(Document)
  144. } // namespace Lua
  145. } // namespace Rml