Factory.cpp 23 KB

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