Controls.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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/Controls/Controls.h"
  29. #include "../../Include/RmlUi/Core/ElementInstancer.h"
  30. #include "../../Include/RmlUi/Core/Factory.h"
  31. #include "../../Include/RmlUi/Core/StyleSheetSpecification.h"
  32. #include "../../Include/RmlUi/Core/XMLParser.h"
  33. #include "../../Include/RmlUi/Core/Plugin.h"
  34. #include "../../Include/RmlUi/Core/Core.h"
  35. #include "../../Include/RmlUi/Core/Types.h"
  36. #include "ElementTextSelection.h"
  37. #include "XMLNodeHandlerDataGrid.h"
  38. #include "XMLNodeHandlerTabSet.h"
  39. #include "XMLNodeHandlerTextArea.h"
  40. #include "../../Include/RmlUi/Controls/ElementForm.h"
  41. #include "../../Include/RmlUi/Controls/ElementFormControlInput.h"
  42. #include "../../Include/RmlUi/Controls/ElementFormControlDataSelect.h"
  43. #include "../../Include/RmlUi/Controls/ElementFormControlSelect.h"
  44. #include "../../Include/RmlUi/Controls/ElementFormControlSelect.h"
  45. #include "../../Include/RmlUi/Controls/ElementFormControlTextArea.h"
  46. #include "../../Include/RmlUi/Controls/ElementTabSet.h"
  47. #include "../../Include/RmlUi/Controls/ElementProgressBar.h"
  48. #include "../../Include/RmlUi/Controls/ElementDataGrid.h"
  49. #include "../../Include/RmlUi/Controls/ElementDataGridExpandButton.h"
  50. #include "../../Include/RmlUi/Controls/ElementDataGridCell.h"
  51. #include "../../Include/RmlUi/Controls/ElementDataGridRow.h"
  52. namespace Rml {
  53. namespace Controls {
  54. struct ElementInstancers {
  55. using Ptr = std::unique_ptr<Core::ElementInstancer>;
  56. template<typename T> using ElementInstancerGeneric = Core::ElementInstancerGeneric<T>;
  57. Ptr form = std::make_unique<ElementInstancerGeneric<ElementForm>>();
  58. Ptr input = std::make_unique<ElementInstancerGeneric<ElementFormControlInput>>();
  59. Ptr dataselect = std::make_unique<ElementInstancerGeneric<ElementFormControlDataSelect>>();
  60. Ptr select = std::make_unique<ElementInstancerGeneric<ElementFormControlSelect>>();
  61. Ptr textarea = std::make_unique<ElementInstancerGeneric<ElementFormControlTextArea>>();
  62. Ptr selection = std::make_unique<ElementInstancerGeneric<ElementTextSelection>>();
  63. Ptr tabset = std::make_unique<ElementInstancerGeneric<ElementTabSet>>();
  64. Ptr progressbar = std::make_unique<ElementInstancerGeneric<ElementProgressBar>>();
  65. Ptr datagrid = std::make_unique<ElementInstancerGeneric<ElementDataGrid>>();
  66. Ptr datagrid_expand = std::make_unique<ElementInstancerGeneric<ElementDataGridExpandButton>>();
  67. Ptr datagrid_cell = std::make_unique<ElementInstancerGeneric<ElementDataGridCell>>();
  68. Ptr datagrid_row = std::make_unique<ElementInstancerGeneric<ElementDataGridRow>>();
  69. };
  70. static std::unique_ptr<ElementInstancers> element_instancers;
  71. // Registers the custom element instancers.
  72. void RegisterElementInstancers()
  73. {
  74. element_instancers = std::make_unique<ElementInstancers>();
  75. Core::Factory::RegisterElementInstancer("form", element_instancers->form.get());
  76. Core::Factory::RegisterElementInstancer("input", element_instancers->input.get());
  77. Core::Factory::RegisterElementInstancer("dataselect", element_instancers->dataselect.get());
  78. Core::Factory::RegisterElementInstancer("select", element_instancers->select.get());
  79. Core::Factory::RegisterElementInstancer("textarea", element_instancers->textarea.get());
  80. Core::Factory::RegisterElementInstancer("#selection", element_instancers->selection.get());
  81. Core::Factory::RegisterElementInstancer("tabset", element_instancers->tabset.get());
  82. Core::Factory::RegisterElementInstancer("progressbar", element_instancers->progressbar.get());
  83. Core::Factory::RegisterElementInstancer("datagrid", element_instancers->datagrid.get());
  84. Core::Factory::RegisterElementInstancer("datagridexpand", element_instancers->datagrid_expand.get());
  85. Core::Factory::RegisterElementInstancer("#rmlctl_datagridcell", element_instancers->datagrid_cell.get());
  86. Core::Factory::RegisterElementInstancer("#rmlctl_datagridrow", element_instancers->datagrid_row.get());
  87. }
  88. void RegisterXMLNodeHandlers()
  89. {
  90. Core::XMLParser::RegisterNodeHandler("datagrid", std::make_shared<XMLNodeHandlerDataGrid>());
  91. Core::XMLParser::RegisterNodeHandler("tabset", std::make_shared<XMLNodeHandlerTabSet>());
  92. Core::XMLParser::RegisterNodeHandler("textarea", std::make_shared<XMLNodeHandlerTextArea>());
  93. }
  94. static bool initialised = false;
  95. class ControlsPlugin : public Rml::Core::Plugin
  96. {
  97. public:
  98. void OnShutdown() override
  99. {
  100. element_instancers.reset();
  101. initialised = false;
  102. delete this;
  103. }
  104. int GetEventClasses() override
  105. {
  106. return Rml::Core::Plugin::EVT_BASIC;
  107. }
  108. };
  109. void Initialise()
  110. {
  111. // Prevent double initialisation
  112. if (!initialised)
  113. {
  114. // Register the element instancers for our custom elements.
  115. RegisterElementInstancers();
  116. // Register the XML node handlers for our elements that require special parsing.
  117. RegisterXMLNodeHandlers();
  118. // Register the controls plugin, so we'll be notified on Shutdown
  119. Rml::Core::RegisterPlugin(new ControlsPlugin());
  120. initialised = true;
  121. }
  122. }
  123. }
  124. }