Factory.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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 "../../Include/RmlUi/Core/Factory.h"
  29. #include "../../Include/RmlUi/Core/Context.h"
  30. #include "../../Include/RmlUi/Core/ContextInstancer.h"
  31. #include "../../Include/RmlUi/Core/Core.h"
  32. #include "../../Include/RmlUi/Core/ElementDocument.h"
  33. #include "../../Include/RmlUi/Core/ElementInstancer.h"
  34. #include "../../Include/RmlUi/Core/ElementText.h"
  35. #include "../../Include/RmlUi/Core/ElementUtilities.h"
  36. #include "../../Include/RmlUi/Core/EventListenerInstancer.h"
  37. #include "../../Include/RmlUi/Core/StreamMemory.h"
  38. #include "../../Include/RmlUi/Core/StyleSheet.h"
  39. #include "../../Include/RmlUi/Core/StyleSheetContainer.h"
  40. #include "../../Include/RmlUi/Core/SystemInterface.h"
  41. #include "../../Include/RmlUi/Core/Elements/ElementForm.h"
  42. #include "../../Include/RmlUi/Core/Elements/ElementFormControlInput.h"
  43. #include "../../Include/RmlUi/Core/Elements/ElementFormControlSelect.h"
  44. #include "../../Include/RmlUi/Core/Elements/ElementFormControlSelect.h"
  45. #include "../../Include/RmlUi/Core/Elements/ElementFormControlTextArea.h"
  46. #include "../../Include/RmlUi/Core/Elements/ElementTabSet.h"
  47. #include "../../Include/RmlUi/Core/Elements/ElementProgress.h"
  48. #include "ContextInstancerDefault.h"
  49. #include "DataControllerDefault.h"
  50. #include "DataViewDefault.h"
  51. #include "DecoratorTiledBoxInstancer.h"
  52. #include "DecoratorTiledHorizontalInstancer.h"
  53. #include "DecoratorTiledImageInstancer.h"
  54. #include "DecoratorTiledVerticalInstancer.h"
  55. #include "DecoratorNinePatch.h"
  56. #include "DecoratorGradient.h"
  57. #include "ElementHandle.h"
  58. #include "EventInstancerDefault.h"
  59. #include "FontEffectBlur.h"
  60. #include "FontEffectGlow.h"
  61. #include "FontEffectOutline.h"
  62. #include "FontEffectShadow.h"
  63. #include "PluginRegistry.h"
  64. #include "StreamFile.h"
  65. #include "StyleSheetFactory.h"
  66. #include "TemplateCache.h"
  67. #include "XMLNodeHandlerBody.h"
  68. #include "XMLNodeHandlerDefault.h"
  69. #include "XMLNodeHandlerHead.h"
  70. #include "XMLNodeHandlerTemplate.h"
  71. #include "XMLParseTools.h"
  72. #include "Elements/ElementImage.h"
  73. #include "Elements/ElementLabel.h"
  74. #include "Elements/ElementTextSelection.h"
  75. #include "Elements/XMLNodeHandlerSelect.h"
  76. #include "Elements/XMLNodeHandlerTabSet.h"
  77. #include "Elements/XMLNodeHandlerTextArea.h"
  78. #include <algorithm>
  79. namespace Rml {
  80. // Element instancers.
  81. using ElementInstancerMap = UnorderedMap< String, ElementInstancer* >;
  82. static ElementInstancerMap element_instancers;
  83. // Decorator instancers.
  84. using DecoratorInstancerMap = UnorderedMap< String, DecoratorInstancer* >;
  85. static DecoratorInstancerMap decorator_instancers;
  86. // Font effect instancers.
  87. using FontEffectInstancerMap = UnorderedMap< String, FontEffectInstancer* >;
  88. static FontEffectInstancerMap font_effect_instancers;
  89. // Data view instancers.
  90. using DataViewInstancerMap = UnorderedMap< String, DataViewInstancer* >;
  91. static DataViewInstancerMap data_view_instancers;
  92. // Data controller instancers.
  93. using DataControllerInstancerMap = UnorderedMap< String, DataControllerInstancer* >;
  94. static DataControllerInstancerMap data_controller_instancers;
  95. // Structural data view instancers.
  96. using StructuralDataViewInstancerMap = SmallUnorderedMap< String, DataViewInstancer* >;
  97. static StructuralDataViewInstancerMap structural_data_view_instancers;
  98. // Structural data view names.
  99. static StringList structural_data_view_attribute_names;
  100. // The context instancer.
  101. static ContextInstancer* context_instancer = nullptr;
  102. // The event instancer
  103. static EventInstancer* event_instancer = nullptr;
  104. // Event listener instancer.
  105. static EventListenerInstancer* event_listener_instancer = nullptr;
  106. // Default instancers are constructed and destroyed on Initialise and Shutdown, respectively.
  107. struct DefaultInstancers {
  108. UniquePtr<ContextInstancer> context_default;
  109. UniquePtr<EventInstancer> event_default;
  110. // Basic elements
  111. ElementInstancerElement element_default;
  112. ElementInstancerText element_text;
  113. ElementInstancerGeneric<ElementImage> element_img;
  114. ElementInstancerGeneric<ElementHandle> element_handle;
  115. ElementInstancerGeneric<ElementDocument> element_body;
  116. // Control elements
  117. ElementInstancerGeneric<ElementForm> form;
  118. ElementInstancerGeneric<ElementFormControlInput> input;
  119. ElementInstancerGeneric<ElementFormControlSelect> select;
  120. ElementInstancerGeneric<ElementLabel> element_label;
  121. ElementInstancerGeneric<ElementFormControlTextArea> textarea;
  122. ElementInstancerGeneric<ElementTextSelection> selection;
  123. ElementInstancerGeneric<ElementTabSet> tabset;
  124. ElementInstancerGeneric<ElementProgress> progress;
  125. // Decorators
  126. DecoratorTiledHorizontalInstancer decorator_tiled_horizontal;
  127. DecoratorTiledVerticalInstancer decorator_tiled_vertical;
  128. DecoratorTiledBoxInstancer decorator_tiled_box;
  129. DecoratorTiledImageInstancer decorator_image;
  130. DecoratorNinePatchInstancer decorator_ninepatch;
  131. DecoratorGradientInstancer decorator_gradient;
  132. // Font effects
  133. FontEffectBlurInstancer font_effect_blur;
  134. FontEffectGlowInstancer font_effect_glow;
  135. FontEffectOutlineInstancer font_effect_outline;
  136. FontEffectShadowInstancer font_effect_shadow;
  137. // Data binding views
  138. DataViewInstancerDefault<DataViewAttribute> data_view_attribute;
  139. DataViewInstancerDefault<DataViewAttributeIf> data_view_attribute_if;
  140. DataViewInstancerDefault<DataViewClass> data_view_class;
  141. DataViewInstancerDefault<DataViewIf> data_view_if;
  142. DataViewInstancerDefault<DataViewVisible> data_view_visible;
  143. DataViewInstancerDefault<DataViewRml> data_view_rml;
  144. DataViewInstancerDefault<DataViewStyle> data_view_style;
  145. DataViewInstancerDefault<DataViewText> data_view_text;
  146. DataViewInstancerDefault<DataViewValue> data_view_value;
  147. DataViewInstancerDefault<DataViewChecked> data_view_checked;
  148. DataViewInstancerDefault<DataViewAlias> data_view_alias;
  149. DataViewInstancerDefault<DataViewFor> structural_data_view_for;
  150. // Data binding controllers
  151. DataControllerInstancerDefault<DataControllerEvent> data_controller_event;
  152. DataControllerInstancerDefault<DataControllerValue> data_controller_value;
  153. };
  154. static UniquePtr<DefaultInstancers> default_instancers;
  155. Factory::Factory()
  156. {
  157. }
  158. Factory::~Factory()
  159. {
  160. }
  161. bool Factory::Initialise()
  162. {
  163. default_instancers = MakeUnique<DefaultInstancers>();
  164. // Default context instancer
  165. if (!context_instancer)
  166. {
  167. default_instancers->context_default = MakeUnique<ContextInstancerDefault>();
  168. context_instancer = default_instancers->context_default.get();
  169. }
  170. // Default event instancer
  171. if (!event_instancer)
  172. {
  173. default_instancers->event_default = MakeUnique<EventInstancerDefault>();
  174. event_instancer = default_instancers->event_default.get();
  175. }
  176. // No default event listener instancer
  177. if (!event_listener_instancer)
  178. event_listener_instancer = nullptr;
  179. // Basic element instancers
  180. RegisterElementInstancer("*", &default_instancers->element_default);
  181. RegisterElementInstancer("img", &default_instancers->element_img);
  182. RegisterElementInstancer("#text", &default_instancers->element_text);
  183. RegisterElementInstancer("handle", &default_instancers->element_handle);
  184. RegisterElementInstancer("body", &default_instancers->element_body);
  185. // Control element instancers
  186. RegisterElementInstancer("form", &default_instancers->form);
  187. RegisterElementInstancer("input", &default_instancers->input);
  188. RegisterElementInstancer("select", &default_instancers->select);
  189. RegisterElementInstancer("label", &default_instancers->element_label);
  190. RegisterElementInstancer("textarea", &default_instancers->textarea);
  191. RegisterElementInstancer("#selection", &default_instancers->selection);
  192. RegisterElementInstancer("tabset", &default_instancers->tabset);
  193. RegisterElementInstancer("progress", &default_instancers->progress);
  194. RegisterElementInstancer("progressbar", &default_instancers->progress);
  195. // Decorator instancers
  196. RegisterDecoratorInstancer("tiled-horizontal", &default_instancers->decorator_tiled_horizontal);
  197. RegisterDecoratorInstancer("tiled-vertical", &default_instancers->decorator_tiled_vertical);
  198. RegisterDecoratorInstancer("tiled-box", &default_instancers->decorator_tiled_box);
  199. RegisterDecoratorInstancer("image", &default_instancers->decorator_image);
  200. RegisterDecoratorInstancer("ninepatch", &default_instancers->decorator_ninepatch);
  201. RegisterDecoratorInstancer("gradient", &default_instancers->decorator_gradient);
  202. // Font effect instancers
  203. RegisterFontEffectInstancer("blur", &default_instancers->font_effect_blur);
  204. RegisterFontEffectInstancer("glow", &default_instancers->font_effect_glow);
  205. RegisterFontEffectInstancer("outline", &default_instancers->font_effect_outline);
  206. RegisterFontEffectInstancer("shadow", &default_instancers->font_effect_shadow);
  207. // Data binding views
  208. RegisterDataViewInstancer(&default_instancers->data_view_attribute, "attr", false);
  209. RegisterDataViewInstancer(&default_instancers->data_view_attribute_if, "attrif", false);
  210. RegisterDataViewInstancer(&default_instancers->data_view_class, "class", false);
  211. RegisterDataViewInstancer(&default_instancers->data_view_if, "if", false);
  212. RegisterDataViewInstancer(&default_instancers->data_view_visible, "visible", false);
  213. RegisterDataViewInstancer(&default_instancers->data_view_rml, "rml", false);
  214. RegisterDataViewInstancer(&default_instancers->data_view_style, "style", false);
  215. RegisterDataViewInstancer(&default_instancers->data_view_text, "text", false);
  216. RegisterDataViewInstancer(&default_instancers->data_view_value, "value", false);
  217. RegisterDataViewInstancer(&default_instancers->data_view_checked, "checked", false);
  218. RegisterDataViewInstancer(&default_instancers->data_view_alias, "alias", false);
  219. RegisterDataViewInstancer(&default_instancers->structural_data_view_for, "for", true );
  220. // Data binding controllers
  221. RegisterDataControllerInstancer(&default_instancers->data_controller_value, "checked");
  222. RegisterDataControllerInstancer(&default_instancers->data_controller_event, "event");
  223. RegisterDataControllerInstancer(&default_instancers->data_controller_value, "value");
  224. // XML node handlers
  225. XMLParser::RegisterNodeHandler("", MakeShared<XMLNodeHandlerDefault>());
  226. XMLParser::RegisterNodeHandler("body", MakeShared<XMLNodeHandlerBody>());
  227. XMLParser::RegisterNodeHandler("head", MakeShared<XMLNodeHandlerHead>());
  228. XMLParser::RegisterNodeHandler("template", MakeShared<XMLNodeHandlerTemplate>());
  229. // XML node handlers for control elements
  230. XMLParser::RegisterNodeHandler("tabset", MakeShared<XMLNodeHandlerTabSet>());
  231. XMLParser::RegisterNodeHandler("textarea", MakeShared<XMLNodeHandlerTextArea>());
  232. XMLParser::RegisterNodeHandler("select", MakeShared<XMLNodeHandlerSelect>());
  233. return true;
  234. }
  235. void Factory::Shutdown()
  236. {
  237. element_instancers.clear();
  238. decorator_instancers.clear();
  239. font_effect_instancers.clear();
  240. data_controller_instancers.clear();
  241. data_view_instancers.clear();
  242. structural_data_view_instancers.clear();
  243. structural_data_view_attribute_names.clear();
  244. context_instancer = nullptr;
  245. event_listener_instancer = nullptr;
  246. event_instancer = nullptr;
  247. XMLParser::ReleaseHandlers();
  248. default_instancers.reset();
  249. }
  250. // Registers the instancer to use when instancing contexts.
  251. void Factory::RegisterContextInstancer(ContextInstancer* instancer)
  252. {
  253. context_instancer = instancer;
  254. }
  255. // Instances a new context.
  256. ContextPtr Factory::InstanceContext(const String& name)
  257. {
  258. ContextPtr new_context = context_instancer->InstanceContext(name);
  259. if (new_context)
  260. new_context->SetInstancer(context_instancer);
  261. return new_context;
  262. }
  263. void Factory::RegisterElementInstancer(const String& name, ElementInstancer* instancer)
  264. {
  265. element_instancers[StringUtilities::ToLower(name)] = instancer;
  266. }
  267. // Looks up the instancer for the given element
  268. ElementInstancer* Factory::GetElementInstancer(const String& tag)
  269. {
  270. ElementInstancerMap::iterator instancer_iterator = element_instancers.find(tag);
  271. if (instancer_iterator == element_instancers.end())
  272. {
  273. instancer_iterator = element_instancers.find("*");
  274. if (instancer_iterator == element_instancers.end())
  275. return nullptr;
  276. }
  277. return instancer_iterator->second;
  278. }
  279. // Instances a single element.
  280. ElementPtr Factory::InstanceElement(Element* parent, const String& instancer_name, const String& tag, const XMLAttributes& attributes)
  281. {
  282. if (ElementInstancer* instancer = GetElementInstancer(instancer_name))
  283. {
  284. if (ElementPtr element = instancer->InstanceElement(parent, tag, attributes))
  285. {
  286. element->SetInstancer(instancer);
  287. element->SetAttributes(attributes);
  288. PluginRegistry::NotifyElementCreate(element.get());
  289. return element;
  290. }
  291. }
  292. return nullptr;
  293. }
  294. // Instances a single text element containing a string.
  295. bool Factory::InstanceElementText(Element* parent, const String& in_text)
  296. {
  297. RMLUI_ASSERT(parent);
  298. String text;
  299. if (SystemInterface* system_interface = GetSystemInterface())
  300. system_interface->TranslateString(text, in_text);
  301. // If this text node only contains white-space we don't want to construct it.
  302. const bool only_white_space = std::all_of(text.begin(), text.end(), &StringUtilities::IsWhitespace);
  303. if (only_white_space)
  304. return true;
  305. // See if we need to parse it as RML, and whether the text contains data expressions (curly brackets).
  306. bool parse_as_rml = false;
  307. bool has_data_expression = false;
  308. bool inside_brackets = false;
  309. bool inside_string = false;
  310. char previous = 0;
  311. for (const char c : text)
  312. {
  313. const char* error_str = XMLParseTools::ParseDataBrackets(inside_brackets, inside_string, c, previous);
  314. if (error_str)
  315. {
  316. Log::Message(Log::LT_WARNING, "Failed to instance text element '%s'. %s", text.c_str(), error_str);
  317. return false;
  318. }
  319. if (inside_brackets)
  320. has_data_expression = true;
  321. else if (c == '<')
  322. parse_as_rml = true;
  323. previous = c;
  324. }
  325. // If the text contains RML elements then run it through the XML parser again.
  326. if (parse_as_rml)
  327. {
  328. RMLUI_ZoneScopedNC("InstanceStream", 0xDC143C);
  329. auto stream = MakeUnique<StreamMemory>(text.size() + 32);
  330. Context* context = parent->GetContext();
  331. String tag = context ? context->GetDocumentsBaseTag() : "body";
  332. String open_tag = "<" + tag + ">";
  333. String close_tag = "</" + tag + ">";
  334. stream->Write(open_tag.c_str(), open_tag.size());
  335. stream->Write(text);
  336. stream->Write(close_tag.c_str(), close_tag.size());
  337. stream->Seek(0, SEEK_SET);
  338. InstanceElementStream(parent, stream.get());
  339. }
  340. else
  341. {
  342. RMLUI_ZoneScopedNC("InstanceText", 0x8FBC8F);
  343. // Attempt to instance the element.
  344. XMLAttributes attributes;
  345. // If we have curly brackets in the text, we tag the element so that the appropriate data view (DataViewText) is constructed.
  346. if (has_data_expression)
  347. attributes.emplace("data-text", Variant());
  348. ElementPtr element = Factory::InstanceElement(parent, "#text", "#text", attributes);
  349. if (!element)
  350. {
  351. Log::Message(Log::LT_ERROR, "Failed to instance text element '%s', instancer returned nullptr.", text.c_str());
  352. return false;
  353. }
  354. // Assign the element its text value.
  355. ElementText* text_element = rmlui_dynamic_cast< ElementText* >(element.get());
  356. if (!text_element)
  357. {
  358. Log::Message(Log::LT_ERROR, "Failed to instance text element '%s'. Found type '%s', was expecting a derivative of ElementText.", text.c_str(), rmlui_type_name(*element));
  359. return false;
  360. }
  361. // Unescape any escaped entities or unicode symbols
  362. text = StringUtilities::DecodeRml(text);
  363. text_element->SetText(text);
  364. // Add to active node.
  365. parent->AppendChild(std::move(element));
  366. }
  367. return true;
  368. }
  369. // Instances a element tree based on the stream
  370. bool Factory::InstanceElementStream(Element* parent, Stream* stream)
  371. {
  372. XMLParser parser(parent);
  373. parser.Parse(stream);
  374. return true;
  375. }
  376. // Instances a element tree based on the stream
  377. ElementPtr Factory::InstanceDocumentStream(Context* context, Stream* stream, const String& document_base_tag)
  378. {
  379. RMLUI_ZoneScoped;
  380. ElementPtr element = Factory::InstanceElement(nullptr, document_base_tag, document_base_tag, XMLAttributes());
  381. if (!element)
  382. {
  383. Log::Message(Log::LT_ERROR, "Failed to instance document, instancer returned nullptr.");
  384. return nullptr;
  385. }
  386. ElementDocument* document = rmlui_dynamic_cast< ElementDocument* >(element.get());
  387. if (!document)
  388. {
  389. Log::Message(Log::LT_ERROR, "Failed to instance document element. Found type '%s', was expecting derivative of ElementDocument.", rmlui_type_name(*element));
  390. return nullptr;
  391. }
  392. document->context = context;
  393. XMLParser parser(element.get());
  394. parser.Parse(stream);
  395. return element;
  396. }
  397. // Registers an instancer that will be used to instance decorators.
  398. void Factory::RegisterDecoratorInstancer(const String& name, DecoratorInstancer* instancer)
  399. {
  400. RMLUI_ASSERT(instancer);
  401. decorator_instancers[StringUtilities::ToLower(name)] = instancer;
  402. }
  403. // Retrieves a decorator instancer registered with the factory.
  404. DecoratorInstancer* Factory::GetDecoratorInstancer(const String& name)
  405. {
  406. auto iterator = decorator_instancers.find(name);
  407. if (iterator == decorator_instancers.end())
  408. return nullptr;
  409. return iterator->second;
  410. }
  411. // Registers an instancer that will be used to instance font effects.
  412. void Factory::RegisterFontEffectInstancer(const String& name, FontEffectInstancer* instancer)
  413. {
  414. RMLUI_ASSERT(instancer);
  415. font_effect_instancers[StringUtilities::ToLower(name)] = instancer;
  416. }
  417. FontEffectInstancer* Factory::GetFontEffectInstancer(const String& name)
  418. {
  419. auto iterator = font_effect_instancers.find(name);
  420. if (iterator == font_effect_instancers.end())
  421. return nullptr;
  422. return iterator->second;
  423. }
  424. // Creates a style sheet containing the passed in styles.
  425. SharedPtr<StyleSheetContainer> Factory::InstanceStyleSheetString(const String& string)
  426. {
  427. auto memory_stream = MakeUnique<StreamMemory>((const byte*) string.c_str(), string.size());
  428. return InstanceStyleSheetStream(memory_stream.get());
  429. }
  430. // Creates a style sheet from a file.
  431. SharedPtr<StyleSheetContainer> Factory::InstanceStyleSheetFile(const String& file_name)
  432. {
  433. auto file_stream = MakeUnique<StreamFile>();
  434. file_stream->Open(file_name);
  435. return InstanceStyleSheetStream(file_stream.get());
  436. }
  437. // Creates a style sheet from an Stream.
  438. SharedPtr<StyleSheetContainer> Factory::InstanceStyleSheetStream(Stream* stream)
  439. {
  440. SharedPtr<StyleSheetContainer> style_sheet_container = MakeShared<StyleSheetContainer>();
  441. if (style_sheet_container->LoadStyleSheetContainer(stream))
  442. {
  443. return style_sheet_container;
  444. }
  445. return nullptr;
  446. }
  447. // Clears the style sheet cache. This will force style sheets to be reloaded.
  448. void Factory::ClearStyleSheetCache()
  449. {
  450. StyleSheetFactory::ClearStyleSheetCache();
  451. }
  452. /// Clears the template cache. This will force templates to be reloaded.
  453. void Factory::ClearTemplateCache()
  454. {
  455. TemplateCache::Clear();
  456. }
  457. // Registers an instancer for all RmlEvents
  458. void Factory::RegisterEventInstancer(EventInstancer* instancer)
  459. {
  460. event_instancer = instancer;
  461. }
  462. // Instance an event object.
  463. EventPtr Factory::InstanceEvent(Element* target, EventId id, const String& type, const Dictionary& parameters, bool interruptible)
  464. {
  465. EventPtr event = event_instancer->InstanceEvent(target, id, type, parameters, interruptible);
  466. if (event)
  467. event->instancer = event_instancer;
  468. return event;
  469. }
  470. // Register an instancer for all event listeners
  471. void Factory::RegisterEventListenerInstancer(EventListenerInstancer* instancer)
  472. {
  473. event_listener_instancer = instancer;
  474. }
  475. // Instance an event listener with the given string
  476. EventListener* Factory::InstanceEventListener(const String& value, Element* element)
  477. {
  478. // If we have an event listener instancer, use it
  479. if (event_listener_instancer)
  480. return event_listener_instancer->InstanceEventListener(value, element);
  481. return nullptr;
  482. }
  483. void Factory::RegisterDataViewInstancer(DataViewInstancer* instancer, const String& name, bool is_structural_view)
  484. {
  485. bool inserted = false;
  486. if (is_structural_view)
  487. {
  488. inserted = structural_data_view_instancers.emplace(name, instancer).second;
  489. if (inserted)
  490. structural_data_view_attribute_names.push_back(String("data-") + name);
  491. }
  492. else
  493. {
  494. inserted = data_view_instancers.emplace(name, instancer).second;
  495. }
  496. if (!inserted)
  497. Log::Message(Log::LT_WARNING, "Could not register data view instancer '%s'. The given name is already registered.", name.c_str());
  498. }
  499. void Factory::RegisterDataControllerInstancer(DataControllerInstancer* instancer, const String& name)
  500. {
  501. bool inserted = data_controller_instancers.emplace(name, instancer).second;
  502. if (!inserted)
  503. Log::Message(Log::LT_WARNING, "Could not register data controller instancer '%s'. The given name is already registered.", name.c_str());
  504. }
  505. DataViewPtr Factory::InstanceDataView(const String& type_name, Element* element, bool is_structural_view)
  506. {
  507. RMLUI_ASSERT(element);
  508. if (is_structural_view)
  509. {
  510. auto it = structural_data_view_instancers.find(type_name);
  511. if (it != structural_data_view_instancers.end())
  512. return it->second->InstanceView(element);
  513. }
  514. else
  515. {
  516. auto it = data_view_instancers.find(type_name);
  517. if (it != data_view_instancers.end())
  518. return it->second->InstanceView(element);
  519. }
  520. return nullptr;
  521. }
  522. DataControllerPtr Factory::InstanceDataController(const String& type_name, Element* element)
  523. {
  524. auto it = data_controller_instancers.find(type_name);
  525. if (it != data_controller_instancers.end())
  526. return it->second->InstanceController(element);
  527. return DataControllerPtr();
  528. }
  529. bool Factory::IsStructuralDataView(const String& type_name)
  530. {
  531. return structural_data_view_instancers.find(type_name) != structural_data_view_instancers.end();
  532. }
  533. const StringList& Factory::GetStructuralDataViewAttributeNames()
  534. {
  535. return structural_data_view_attribute_names;
  536. }
  537. } // namespace Rml