DataControllerDefault.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. #include "DataControllerDefault.h"
  29. #include "../../Include/RmlUi/Core/Element.h"
  30. #include "DataController.h"
  31. #include "DataExpression.h"
  32. #include "DataModel.h"
  33. #include "EventSpecification.h"
  34. namespace Rml {
  35. DataControllerValue::DataControllerValue(Element* element) : DataController(element) {}
  36. DataControllerValue::~DataControllerValue()
  37. {
  38. if (Element* element = GetElement())
  39. element->RemoveEventListener(EventId::Change, this);
  40. }
  41. bool DataControllerValue::Initialize(DataModel& model, Element* element, const String& variable_name, const String& /*modifier*/)
  42. {
  43. RMLUI_ASSERT(element);
  44. DataAddress variable_address = model.ResolveAddress(variable_name, element);
  45. if (variable_address.empty())
  46. return false;
  47. if (model.GetVariable(variable_address))
  48. address = std::move(variable_address);
  49. element->AddEventListener(EventId::Change, this);
  50. return true;
  51. }
  52. void DataControllerValue::ProcessEvent(Event& event)
  53. {
  54. if (const Element* element = GetElement())
  55. {
  56. Variant value_to_set;
  57. const auto& parameters = event.GetParameters();
  58. const auto override_value_it = parameters.find("data-binding-override-value");
  59. const auto value_it = parameters.find("value");
  60. if (override_value_it != parameters.cend())
  61. value_to_set = override_value_it->second;
  62. else if (value_it != parameters.cend())
  63. value_to_set = value_it->second;
  64. else
  65. Log::Message(Log::LT_WARNING,
  66. "A 'change' event was received, but it did not contain the attribute 'value' when processing a data binding in %s",
  67. element->GetAddress().c_str());
  68. DataModel* model = element->GetDataModel();
  69. if (value_to_set.GetType() == Variant::NONE || !model)
  70. return;
  71. if (DataVariable variable = model->GetVariable(address))
  72. if (variable.Set(value_to_set))
  73. model->DirtyVariable(address.front().name);
  74. }
  75. }
  76. void DataControllerValue::Release()
  77. {
  78. delete this;
  79. }
  80. DataControllerEvent::DataControllerEvent(Element* element) : DataController(element) {}
  81. DataControllerEvent::~DataControllerEvent()
  82. {
  83. if (Element* element = GetElement())
  84. {
  85. if (id != EventId::Invalid)
  86. element->RemoveEventListener(id, this);
  87. }
  88. }
  89. bool DataControllerEvent::Initialize(DataModel& model, Element* element, const String& expression_str, const String& modifier)
  90. {
  91. RMLUI_ASSERT(element);
  92. expression = MakeUnique<DataExpression>(expression_str);
  93. DataExpressionInterface expr_interface(&model, element);
  94. if (!expression->Parse(expr_interface, true))
  95. return false;
  96. id = EventSpecificationInterface::GetIdOrInsert(modifier);
  97. if (id == EventId::Invalid)
  98. {
  99. Log::Message(Log::LT_WARNING, "Event type '%s' could not be recognized, while adding 'data-event' to %s", modifier.c_str(),
  100. element->GetAddress().c_str());
  101. return false;
  102. }
  103. element->AddEventListener(id, this);
  104. return true;
  105. }
  106. void DataControllerEvent::ProcessEvent(Event& event)
  107. {
  108. if (!expression)
  109. return;
  110. if (Element* element = GetElement())
  111. {
  112. DataExpressionInterface expr_interface(element->GetDataModel(), element, &event);
  113. Variant unused_value_out;
  114. expression->Run(expr_interface, unused_value_out);
  115. }
  116. }
  117. void DataControllerEvent::Release()
  118. {
  119. delete this;
  120. }
  121. } // namespace Rml