Window.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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 "Window.h"
  29. #include "../Common/TestsShell.h"
  30. #include <RmlUi/Core/Context.h>
  31. #include <RmlUi/Core/Core.h>
  32. #include <RmlUi/Core/Element.h>
  33. #include <RmlUi/Core/ElementDocument.h>
  34. #include <RmlUi/Core/FileInterface.h>
  35. #include <RmlUi/Core/Types.h>
  36. #include <Shell.h>
  37. #include <doctest.h>
  38. using namespace Rml;
  39. Window::Window(Rml::Context* context, TestSuiteList test_suites) : context(context), test_suites(std::move(test_suites))
  40. {
  41. InitializeXmlNodeHandlers(&meta_list, &link_list);
  42. const String local_data_path_prefix = "/../Tests/Data/";
  43. document_description = context->LoadDocument(local_data_path_prefix + "description.rml");
  44. REQUIRE(document_description);
  45. document_description->Show();
  46. document_source = context->LoadDocument(local_data_path_prefix + "view_source.rml");
  47. REQUIRE(document_source);
  48. ReloadDocument();
  49. context->GetRootElement()->AddEventListener(Rml::EventId::Keydown, this);
  50. context->GetRootElement()->AddEventListener(Rml::EventId::Textinput, this);
  51. }
  52. Window::~Window()
  53. {
  54. context->GetRootElement()->RemoveEventListener(Rml::EventId::Keydown, this);
  55. context->GetRootElement()->RemoveEventListener(Rml::EventId::Textinput, this);
  56. for (ElementDocument* doc : { document, document_description, document_source, document_match })
  57. {
  58. if (doc)
  59. doc->Close();
  60. }
  61. }
  62. const TestSuite& Window::GetCurrentTestSuite() const {
  63. REQUIRE(current_test_suite >= 0);
  64. REQUIRE(current_test_suite < (int)test_suites.size());
  65. return test_suites[current_test_suite];
  66. }
  67. String Window::GetCurrentPath() const {
  68. const TestSuite& test_suite = GetCurrentTestSuite();
  69. REQUIRE(current_id >= 0);
  70. REQUIRE(current_id < (int)test_suite.files.size());
  71. return test_suite.directory + '\\' + test_suite.files[current_id];
  72. }
  73. String Window::GetReferencePath() const {
  74. if (reference_file.empty())
  75. return String();
  76. const TestSuite& test_suite = GetCurrentTestSuite();
  77. return test_suite.directory + '\\' + reference_file;
  78. }
  79. void Window::OpenSource(const String& file_path)
  80. {
  81. FileInterface* file = Rml::GetFileInterface();
  82. FileHandle handle = file->Open(file_path);
  83. if (!handle)
  84. return;
  85. const size_t length = file->Length(handle);
  86. UniquePtr<char[]> buf = UniquePtr<char[]>(new char[length + 1]);
  87. const size_t read_length = file->Read(buf.get(), length, handle);
  88. file->Close(handle);
  89. REQUIRE(read_length > 0);
  90. REQUIRE(read_length <= length);
  91. buf[read_length] = '\0';
  92. const String rml_source = StringUtilities::EncodeRml(String(buf.get()));
  93. Element* element = document_source->GetElementById("code");
  94. REQUIRE(element);
  95. element->SetInnerRML(rml_source);
  96. document_source->Show();
  97. }
  98. void Window::OpenSource()
  99. {
  100. const String file_path = (viewing_reference_source ? GetReferencePath() : GetCurrentPath());
  101. OpenSource(file_path);
  102. }
  103. void Window::SwitchSource()
  104. {
  105. if (document_source->IsVisible())
  106. {
  107. viewing_reference_source = !viewing_reference_source;
  108. OpenSource();
  109. }
  110. }
  111. void Window::CloseSource()
  112. {
  113. document_source->Hide();
  114. viewing_reference_source = false;
  115. }
  116. void Window::ReloadDocument()
  117. {
  118. const String current_path = GetCurrentPath();
  119. const TestSuite& test_suite = GetCurrentTestSuite();
  120. const String& current_filename = test_suite.files[current_id];
  121. if (document)
  122. {
  123. document->Close();
  124. document = nullptr;
  125. }
  126. if (document_match)
  127. {
  128. document_match->Close();
  129. document_match = nullptr;
  130. }
  131. meta_list.clear();
  132. link_list.clear();
  133. document = context->LoadDocument(current_path);
  134. REQUIRE(document);
  135. document->Show();
  136. reference_file.clear();
  137. for (const LinkItem& item : link_list)
  138. {
  139. if (item.rel == "match")
  140. {
  141. reference_file = item.href;
  142. break;
  143. }
  144. }
  145. if (!reference_file.empty())
  146. {
  147. const String reference_path = GetReferencePath();
  148. // See if we can open the file first to avoid logging warnings.
  149. FileInterface* file = Rml::GetFileInterface();
  150. FileHandle handle = file->Open(reference_path);
  151. if (handle)
  152. {
  153. file->Close(handle);
  154. document_match = context->LoadDocument(reference_path);
  155. if (document_match)
  156. {
  157. document_match->SetProperty(PropertyId::Left, Property(510.f, Property::PX));
  158. document_match->Show();
  159. }
  160. }
  161. }
  162. String rml_description = Rml::CreateString(512, "<h1>%s</h1><p>Test %d of %d.<br/>%s", document->GetTitle().c_str(), current_id + 1, (int)test_suite.files.size(), current_filename.c_str());
  163. if (!reference_file.empty())
  164. {
  165. if (document_match)
  166. rml_description += "<br/>" + reference_file;
  167. else
  168. rml_description += "<br/>(X " + reference_file + ")";
  169. }
  170. rml_description += "</p>";
  171. if(!link_list.empty())
  172. {
  173. rml_description += "<p class=\"links\">";
  174. for (const LinkItem& item : link_list)
  175. {
  176. if (item.rel == "match")
  177. continue;
  178. rml_description += "<a href=\"" + item.href + "\">" + item.rel + "</a> ";
  179. }
  180. rml_description += "</p>";
  181. }
  182. for (const MetaItem& item : meta_list)
  183. {
  184. rml_description += "<h3>" + item.name + "</h3>";
  185. rml_description += "<p style=\"min-height: 120px;\">" + item.content + "</p>";
  186. }
  187. Element* description_content = document_description->GetElementById("content");
  188. REQUIRE(description_content);
  189. description_content->SetInnerRML(rml_description);
  190. Element* description_test_suite = document_description->GetElementById("test_suite");
  191. REQUIRE(description_test_suite);
  192. description_test_suite->SetInnerRML(CreateString(64, "Test suite %d of %d", current_test_suite + 1, (int)test_suites.size()));
  193. Element* description_goto = document_description->GetElementById("goto");
  194. REQUIRE(description_goto);
  195. description_goto->SetInnerRML("");
  196. goto_id = 0;
  197. CloseSource();
  198. }
  199. void Window::ProcessEvent(Rml::Event& event)
  200. {
  201. if (event == EventId::Keydown)
  202. {
  203. auto key_identifier = (Rml::Input::KeyIdentifier)event.GetParameter< int >("key_identifier", 0);
  204. bool key_ctrl = event.GetParameter< bool >("ctrl_key", false);
  205. bool key_shift = event.GetParameter< bool >("shift_key", false);
  206. if (key_identifier == Rml::Input::KI_LEFT)
  207. {
  208. current_id = std::max(0, current_id - 1);
  209. ReloadDocument();
  210. }
  211. else if (key_identifier == Rml::Input::KI_RIGHT)
  212. {
  213. current_id = std::min((int)GetCurrentTestSuite().files.size() - 1, current_id + 1);
  214. ReloadDocument();
  215. }
  216. else if (key_identifier == Rml::Input::KI_UP)
  217. {
  218. current_test_suite = std::max(0, current_test_suite - 1);
  219. current_id = 0;
  220. ReloadDocument();
  221. }
  222. else if (key_identifier == Rml::Input::KI_DOWN)
  223. {
  224. current_test_suite = std::min((int)test_suites.size() - 1, current_test_suite + 1);
  225. current_id = 0;
  226. ReloadDocument();
  227. }
  228. else if (key_identifier == Rml::Input::KI_S)
  229. {
  230. if (document_source->IsVisible())
  231. {
  232. if (key_shift)
  233. SwitchSource();
  234. else
  235. CloseSource();
  236. }
  237. else
  238. {
  239. viewing_reference_source = key_shift;
  240. OpenSource();
  241. }
  242. }
  243. else if (key_identifier == Rml::Input::KI_ESCAPE)
  244. {
  245. if (document_source->IsVisible())
  246. CloseSource();
  247. else
  248. TestsShell::RequestExit();
  249. }
  250. else if (key_identifier == Rml::Input::KI_C && key_ctrl)
  251. {
  252. if (key_shift)
  253. Shell::SetClipboardText(GetCurrentTestSuite().directory + '\\' + reference_file);
  254. else
  255. Shell::SetClipboardText(GetCurrentPath());
  256. }
  257. else if (key_identifier == Rml::Input::KI_HOME)
  258. {
  259. current_id = 0;
  260. ReloadDocument();
  261. }
  262. else if (key_identifier == Rml::Input::KI_END)
  263. {
  264. current_id = (int)GetCurrentTestSuite().files.size() - 1;
  265. ReloadDocument();
  266. }
  267. else if (goto_id >= 0 && key_identifier == Rml::Input::KI_BACK)
  268. {
  269. if (goto_id <= 0)
  270. {
  271. goto_id = -1;
  272. document_description->GetElementById("goto")->SetInnerRML("");
  273. }
  274. else
  275. {
  276. goto_id = goto_id / 10;
  277. document_description->GetElementById("goto")->SetInnerRML(CreateString(64, "Go To: %d", goto_id));
  278. }
  279. }
  280. }
  281. if (event == EventId::Textinput)
  282. {
  283. const String text = event.GetParameter< String >("text", "");
  284. for (const char c : text)
  285. {
  286. if (c >= '0' && c <= '9')
  287. {
  288. if (goto_id < 0)
  289. goto_id = 0;
  290. goto_id = goto_id * 10 + int(c - '0');
  291. document_description->GetElementById("goto")->SetInnerRML(CreateString(64, "Go To: %d", goto_id));
  292. }
  293. else if (goto_id >= 0 && c == '\n')
  294. {
  295. if (goto_id > 0 && goto_id <= (int)GetCurrentTestSuite().files.size())
  296. {
  297. current_id = goto_id - 1;
  298. ReloadDocument();
  299. }
  300. else
  301. {
  302. document_description->GetElementById("goto")->SetInnerRML("Error: Go To out of bounds.");
  303. }
  304. goto_id = -1;
  305. }
  306. }
  307. }
  308. }