DataControllerDefault.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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) : DataController(element)
  36. {}
  37. DataControllerValue::~DataControllerValue()
  38. {
  39. if (Element* element = GetElement())
  40. {
  41. element->RemoveEventListener(EventId::Change, this);
  42. }
  43. }
  44. bool DataControllerValue::Initialize(DataModel& model, Element* element, const String& variable_name, const String& /*modifier*/)
  45. {
  46. RMLUI_ASSERT(element);
  47. DataAddress variable_address = model.ResolveAddress(variable_name, element);
  48. if (variable_address.empty())
  49. return false;
  50. if (model.GetVariable(variable_address))
  51. address = std::move(variable_address);
  52. element->AddEventListener(EventId::Change, this);
  53. return true;
  54. }
  55. void DataControllerValue::ProcessEvent(Event& event)
  56. {
  57. if (Element* element = GetElement())
  58. {
  59. const auto& parameters = event.GetParameters();
  60. auto it = parameters.find("value");
  61. if (it == parameters.end())
  62. {
  63. Log::Message(Log::LT_WARNING, "A 'change' event was received, but it did not contain a value. During processing of 'data-value' in %s", element->GetAddress().c_str());
  64. return;
  65. }
  66. DataModel* model = element->GetDataModel();
  67. if (!model)
  68. return;
  69. if (DataVariable variable = model->GetVariable(address))
  70. {
  71. if (SetValue(it->second, variable))
  72. model->DirtyVariable(address.front().name);
  73. }
  74. }
  75. }
  76. void DataControllerValue::Release()
  77. {
  78. delete this;
  79. }
  80. bool DataControllerValue::SetValue(const Variant& value, DataVariable variable)
  81. {
  82. return variable.Set(value);
  83. }
  84. DataControllerChecked::DataControllerChecked(Element* element) : DataControllerValue(element)
  85. {}
  86. bool DataControllerChecked::SetValue(const Variant& value, DataVariable variable)
  87. {
  88. bool result = false;
  89. Variant old_value;
  90. if (variable.Get(old_value))
  91. {
  92. // Value will be empty if the button was just unchecked, otherwise it will take the 'value' attribute.
  93. const String new_value = value.Get<String>();
  94. if (old_value.GetType() == Variant::BOOL)
  95. {
  96. // If the client variable is a boolean type, we assume the button acts like a checkbox, and set the new checked state.
  97. result = variable.Set(Variant(!new_value.empty()));
  98. }
  99. else
  100. {
  101. // Otherwise, we assume the button acts like a radio box. Then, we do nothing if the box was unchecked,
  102. // and instead let only the newly checked box set the new value.
  103. if (!new_value.empty())
  104. result = variable.Set(value);
  105. }
  106. }
  107. return result;
  108. }
  109. DataControllerEvent::DataControllerEvent(Element* element) : DataController(element)
  110. {}
  111. DataControllerEvent::~DataControllerEvent()
  112. {
  113. if (Element* element = GetElement())
  114. {
  115. if (id != EventId::Invalid)
  116. element->RemoveEventListener(id, this);
  117. }
  118. }
  119. bool DataControllerEvent::Initialize(DataModel& model, Element* element, const String& expression_str, const String& modifier)
  120. {
  121. RMLUI_ASSERT(element);
  122. expression = MakeUnique<DataExpression>(expression_str);
  123. DataExpressionInterface expr_interface(&model, element);
  124. if (!expression->Parse(expr_interface, true))
  125. return false;
  126. id = EventSpecificationInterface::GetIdOrInsert(modifier);
  127. if (id == EventId::Invalid)
  128. {
  129. 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());
  130. return false;
  131. }
  132. element->AddEventListener(id, this);
  133. return true;
  134. }
  135. void DataControllerEvent::ProcessEvent(Event& event)
  136. {
  137. if (!expression)
  138. return;
  139. if (Element* element = GetElement())
  140. {
  141. DataExpressionInterface expr_interface(element->GetDataModel(), element, &event);
  142. Variant unused_value_out;
  143. expression->Run(expr_interface, unused_value_out);
  144. }
  145. }
  146. void DataControllerEvent::Release()
  147. {
  148. delete this;
  149. }
  150. } // namespace Rml