Document.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 "Document.h"
  29. #include <Rocket/Core/ElementDocument.h>
  30. #include <Rocket/Core/Context.h>
  31. #include "Element.h"
  32. #include <Rocket/Core/Lua/Utilities.h>
  33. namespace Rocket {
  34. namespace Core {
  35. namespace Lua {
  36. template<> void ExtraInit<Document>(lua_State* L, int metatable_index)
  37. {
  38. //we will inherit from Element
  39. ExtraInit<Element>(L,metatable_index);
  40. LuaType<Element>::_regfunctions(L,metatable_index,metatable_index - 1);
  41. AddTypeToElementAsTable<Document>(L);
  42. //create the DocumentFocus table
  43. lua_getglobal(L,"DocumentFocus");
  44. if(lua_isnoneornil(L,-1))
  45. {
  46. lua_pop(L,1); //pop unsucessful getglobal
  47. lua_newtable(L); //create a table for holding the enum
  48. lua_pushinteger(L,ElementDocument::NONE);
  49. lua_setfield(L,-2,"NONE");
  50. lua_pushinteger(L,ElementDocument::FOCUS);
  51. lua_setfield(L,-2,"FOCUS");
  52. lua_pushinteger(L,ElementDocument::MODAL);
  53. lua_setfield(L,-2,"MODAL");
  54. lua_setglobal(L,"DocumentFocus");
  55. }
  56. }
  57. //methods
  58. int DocumentPullToFront(lua_State* L, Document* obj)
  59. {
  60. obj->PullToFront();
  61. return 0;
  62. }
  63. int DocumentPushToBack(lua_State* L, Document* obj)
  64. {
  65. obj->PushToBack();
  66. return 0;
  67. }
  68. int DocumentShow(lua_State* L, Document* obj)
  69. {
  70. int top = lua_gettop(L);
  71. if(top == 0)
  72. obj->Show();
  73. else
  74. {
  75. int flag = luaL_checkinteger(L,1);
  76. obj->Show(flag);
  77. }
  78. return 0;
  79. }
  80. int DocumentHide(lua_State* L, Document* obj)
  81. {
  82. obj->Hide();
  83. return 0;
  84. }
  85. int DocumentClose(lua_State* L, Document* obj)
  86. {
  87. obj->Close();
  88. return 0;
  89. }
  90. int DocumentCreateElement(lua_State* L, Document* obj)
  91. {
  92. const char* tag = luaL_checkstring(L,1);
  93. Element* ele = obj->CreateElement(tag);
  94. LuaType<Element>::push(L,ele,false);
  95. return 1;
  96. }
  97. int DocumentCreateTextNode(lua_State* L, Document* obj)
  98. {
  99. //need ElementText object first
  100. const char* text = luaL_checkstring(L,1);
  101. ElementText* et = obj->CreateTextNode(text);
  102. LuaType<ElementText>::push(L, et, false);
  103. return 1;
  104. }
  105. //getters
  106. int DocumentGetAttrtitle(lua_State* L)
  107. {
  108. Document* doc = LuaType<Document>::check(L,1);
  109. LUACHECKOBJ(doc);
  110. lua_pushstring(L,doc->GetTitle().CString());
  111. return 1;
  112. }
  113. int DocumentGetAttrcontext(lua_State* L)
  114. {
  115. Document* doc = LuaType<Document>::check(L,1);
  116. LUACHECKOBJ(doc);
  117. LuaType<Context>::push(L,doc->GetContext(),false);
  118. return 1;
  119. }
  120. //setters
  121. int DocumentSetAttrtitle(lua_State* L)
  122. {
  123. Document* doc = LuaType<Document>::check(L,1);
  124. LUACHECKOBJ(doc);
  125. const char* title = luaL_checkstring(L,2);
  126. doc->SetTitle(title);
  127. return 0;
  128. }
  129. RegType<Document> DocumentMethods[] =
  130. {
  131. LUAMETHOD(Document,PullToFront)
  132. LUAMETHOD(Document,PushToBack)
  133. LUAMETHOD(Document,Show)
  134. LUAMETHOD(Document,Hide)
  135. LUAMETHOD(Document,Close)
  136. LUAMETHOD(Document,CreateElement)
  137. LUAMETHOD(Document,CreateTextNode)
  138. { NULL, NULL },
  139. };
  140. luaL_Reg DocumentGetters[] =
  141. {
  142. LUAGETTER(Document,title)
  143. LUAGETTER(Document,context)
  144. { NULL, NULL },
  145. };
  146. luaL_Reg DocumentSetters[] =
  147. {
  148. LUASETTER(Document,title)
  149. { NULL, NULL },
  150. };
  151. LUACORETYPEDEFINE(Document,true)
  152. }
  153. }
  154. }