DataView.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019-2023 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #ifndef RMLUI_CORE_DATAVIEW_H
  29. #define RMLUI_CORE_DATAVIEW_H
  30. #include "../../Include/RmlUi/Core/DataTypes.h"
  31. #include "../../Include/RmlUi/Core/Header.h"
  32. #include "../../Include/RmlUi/Core/Traits.h"
  33. #include "../../Include/RmlUi/Core/Types.h"
  34. namespace Rml {
  35. class Element;
  36. class DataModel;
  37. class DataViewInstancer : public NonCopyMoveable {
  38. public:
  39. DataViewInstancer() {}
  40. virtual ~DataViewInstancer() {}
  41. virtual DataViewPtr InstanceView(Element* element) = 0;
  42. };
  43. template <typename T>
  44. class DataViewInstancerDefault final : public DataViewInstancer {
  45. public:
  46. DataViewPtr InstanceView(Element* element) override { return DataViewPtr(new T(element)); }
  47. };
  48. /**
  49. Data view.
  50. Data views are used to present a data variable in the document by different means.
  51. A data view is declared in the document by the element attribute:
  52. data-[type]-[modifier]="[expression]"
  53. The modifier may or may not be required depending on the data view.
  54. */
  55. class DataView : public Releasable {
  56. public:
  57. virtual ~DataView();
  58. // Initialize the data view.
  59. // @param[in] model The data model the view will be attached to.
  60. // @param[in] element The element which spawned the view.
  61. // @param[in] expression The value of the element's 'data-' attribute which spawned the view (see class documentation).
  62. // @param[in] modifier The modifier for the given view type (see class documentation).
  63. // @return True on success.
  64. virtual bool Initialize(DataModel& model, Element* element, const String& expression, const String& modifier) = 0;
  65. // Update the data view.
  66. // Returns true if the update resulted in a document change.
  67. virtual bool Update(DataModel& model) = 0;
  68. // Returns the list of data variable name(s) which can modify this view.
  69. virtual StringList GetVariableNameList() const = 0;
  70. // Returns the attached element if it still exists.
  71. Element* GetElement() const;
  72. // Data views are first sorted by the depth of the attached element in the
  73. // document tree, then optionally by an offset specified for each data view.
  74. int GetSortOrder() const;
  75. // Returns true if the element still exists.
  76. bool IsValid() const;
  77. protected:
  78. // @param[in] element The element this data view is attached to.
  79. // @param[in] sort_offset A number [-1000, 999] specifying the update order of this
  80. // data view at the same tree depth, negative numbers are updated first.
  81. DataView(Element* element, int sort_offset);
  82. private:
  83. ObserverPtr<Element> attached_element;
  84. int sort_order;
  85. };
  86. class DataViews : NonCopyMoveable {
  87. public:
  88. DataViews();
  89. ~DataViews();
  90. void Add(DataViewPtr view);
  91. void OnElementRemove(Element* element);
  92. bool Update(DataModel& model, const DirtyVariables& dirty_variables);
  93. private:
  94. using DataViewList = Vector<DataViewPtr>;
  95. DataViewList views;
  96. DataViewList views_to_add;
  97. DataViewList views_to_remove;
  98. using NameViewMap = UnorderedMultimap<String, DataView*>;
  99. NameViewMap name_view_map;
  100. };
  101. } // namespace Rml
  102. #endif