DataControllerDefault.cpp 4.4 KB

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