Factory.cpp 23 KB

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