Factory.cpp 21 KB

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