Factory.cpp 23 KB

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