WidgetDropDown.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 "WidgetDropDown.h"
  29. #include "../../Include/RmlUi/Core/Math.h"
  30. #include "../../Include/RmlUi/Core/Factory.h"
  31. #include "../../Include/RmlUi/Core/ElementUtilities.h"
  32. #include "../../Include/RmlUi/Core/Event.h"
  33. #include "../../Include/RmlUi/Core/Input.h"
  34. #include "../../Include/RmlUi/Core/Property.h"
  35. #include "../../Include/RmlUi/Core/Profiling.h"
  36. #include "../../Include/RmlUi/Controls/ElementFormControl.h"
  37. namespace Rml {
  38. namespace Controls {
  39. WidgetDropDown::WidgetDropDown(ElementFormControl* element)
  40. {
  41. parent_element = element;
  42. box_layout_dirty = false;
  43. value_layout_dirty = false;
  44. box_visible = false;
  45. selected_option = -1;
  46. // Create the button and selection elements.
  47. button_element = parent_element->AppendChild(Core::Factory::InstanceElement(parent_element, "*", "selectarrow", Rml::Core::XMLAttributes()), false);
  48. value_element = parent_element->AppendChild(Core::Factory::InstanceElement(parent_element, "*", "selectvalue", Rml::Core::XMLAttributes()), false);
  49. selection_element = parent_element->AppendChild(Core::Factory::InstanceElement(parent_element, "*", "selectbox", Rml::Core::XMLAttributes()), false);
  50. value_element->SetProperty(Core::PropertyId::OverflowX, Core::Property(Core::Style::Overflow::Hidden));
  51. value_element->SetProperty(Core::PropertyId::OverflowY, Core::Property(Core::Style::Overflow::Hidden));
  52. selection_element->SetProperty(Core::PropertyId::Visibility, Core::Property(Core::Style::Visibility::Hidden));
  53. selection_element->SetProperty(Core::PropertyId::ZIndex, Core::Property(1.0f, Core::Property::NUMBER));
  54. selection_element->SetProperty(Core::PropertyId::Clip, Core::Property(Core::Style::Clip::Type::None));
  55. parent_element->AddEventListener(Core::EventId::Click, this, true);
  56. parent_element->AddEventListener(Core::EventId::Blur, this);
  57. parent_element->AddEventListener(Core::EventId::Focus, this);
  58. parent_element->AddEventListener(Core::EventId::Keydown, this, true);
  59. }
  60. WidgetDropDown::~WidgetDropDown()
  61. {
  62. // We shouldn't clear the options ourselves, as removing the element will automatically clear children.
  63. // However, we do need to remove events of children.
  64. for(auto& option : options)
  65. option.GetElement()->RemoveEventListener(Core::EventId::Click, this);
  66. parent_element->RemoveEventListener(Core::EventId::Click, this, true);
  67. parent_element->RemoveEventListener(Core::EventId::Blur, this);
  68. parent_element->RemoveEventListener(Core::EventId::Focus, this);
  69. parent_element->RemoveEventListener(Core::EventId::Keydown, this, true);
  70. }
  71. // Updates the selection box layout if necessary.
  72. void WidgetDropDown::OnRender()
  73. {
  74. if (box_visible && box_layout_dirty)
  75. {
  76. Core::Box box;
  77. Core::ElementUtilities::BuildBox(box, parent_element->GetBox().GetSize(), selection_element);
  78. // Layout the selection box.
  79. Core::ElementUtilities::FormatElement(selection_element, parent_element->GetBox().GetSize(Core::Box::BORDER));
  80. selection_element->SetOffset(Rml::Core::Vector2f(box.GetEdge(Core::Box::MARGIN, Core::Box::LEFT), parent_element->GetBox().GetSize(Core::Box::BORDER).y + box.GetEdge(Core::Box::MARGIN, Core::Box::TOP)), parent_element);
  81. box_layout_dirty = false;
  82. }
  83. if (value_layout_dirty)
  84. {
  85. Core::ElementUtilities::FormatElement(value_element, parent_element->GetBox().GetSize(Core::Box::BORDER));
  86. value_element->SetOffset(parent_element->GetBox().GetPosition(Core::Box::CONTENT), parent_element);
  87. value_layout_dirty = false;
  88. }
  89. }
  90. void WidgetDropDown::OnLayout()
  91. {
  92. RMLUI_ZoneScopedNC("DropDownLayout", 0x7FFF00);
  93. if(parent_element->IsDisabled())
  94. {
  95. // Propagate disabled state to selectvalue and selectarrow
  96. value_element->SetPseudoClass("disabled", true);
  97. button_element->SetPseudoClass("disabled", true);
  98. }
  99. // Layout the button and selection boxes.
  100. Core::Box parent_box = parent_element->GetBox();
  101. Core::ElementUtilities::PositionElement(button_element, Rml::Core::Vector2f(0, 0), Core::ElementUtilities::TOP_RIGHT);
  102. Core::ElementUtilities::PositionElement(selection_element, Rml::Core::Vector2f(0, 0), Core::ElementUtilities::TOP_LEFT);
  103. // Calculate the value element position and size.
  104. Rml::Core::Vector2f size;
  105. size.x = parent_element->GetBox().GetSize(Core::Box::CONTENT).x - button_element->GetBox().GetSize(Core::Box::MARGIN).x;
  106. size.y = parent_element->GetBox().GetSize(Core::Box::CONTENT).y;
  107. value_element->SetOffset(parent_element->GetBox().GetPosition(Core::Box::CONTENT), parent_element);
  108. value_element->SetBox(Core::Box(size));
  109. box_layout_dirty = true;
  110. value_layout_dirty = true;
  111. }
  112. // Sets the value of the widget.
  113. void WidgetDropDown::SetValue(const Rml::Core::String& _value)
  114. {
  115. for (size_t i = 0; i < options.size(); ++i)
  116. {
  117. if (options[i].GetValue() == _value)
  118. {
  119. SetSelection((int) i);
  120. return;
  121. }
  122. }
  123. if (selected_option >= 0 && selected_option < (int)options.size())
  124. options[selected_option].GetElement()->SetPseudoClass("checked", false);
  125. value = _value;
  126. value_element->SetInnerRML(value);
  127. value_layout_dirty = true;
  128. selected_option = -1;
  129. }
  130. // Returns the current value of the widget.
  131. const Rml::Core::String& WidgetDropDown::GetValue() const
  132. {
  133. return value;
  134. }
  135. // Sets the index of the selection. If the new index lies outside of the bounds, it will be clamped.
  136. void WidgetDropDown::SetSelection(int selection, bool force)
  137. {
  138. Rml::Core::String new_value;
  139. if (selection < 0 ||
  140. selection >= (int) options.size())
  141. {
  142. selection = -1;
  143. }
  144. else
  145. {
  146. new_value = options[selection].GetValue();
  147. }
  148. if (force ||
  149. selection != selected_option ||
  150. value != new_value)
  151. {
  152. if (selected_option >= 0 && selected_option < (int)options.size())
  153. options[selected_option].GetElement()->SetPseudoClass("checked", false);
  154. selected_option = selection;
  155. value = new_value;
  156. Rml::Core::String value_rml;
  157. if (selected_option >= 0)
  158. {
  159. auto* el = options[selected_option].GetElement();
  160. el->GetInnerRML(value_rml);
  161. el->SetPseudoClass("checked", true);
  162. }
  163. value_element->SetInnerRML(value_rml);
  164. value_layout_dirty = true;
  165. Rml::Core::Dictionary parameters;
  166. parameters["value"] = value;
  167. parent_element->DispatchEvent(Core::EventId::Change, parameters);
  168. }
  169. }
  170. // Returns the index of the currently selected item.
  171. int WidgetDropDown::GetSelection() const
  172. {
  173. return selected_option;
  174. }
  175. // Adds a new option to the select control.
  176. int WidgetDropDown::AddOption(const Rml::Core::String& rml, const Rml::Core::String& new_value, int before, bool select, bool selectable)
  177. {
  178. Core::ElementPtr element = Core::Factory::InstanceElement(selection_element, "*", "option", Rml::Core::XMLAttributes());
  179. element->SetInnerRML(rml);
  180. int result = AddOption(std::move(element), new_value, before, select, selectable);
  181. return result;
  182. }
  183. int WidgetDropDown::AddOption(Rml::Core::ElementPtr element, const Rml::Core::String& new_value, int before, bool select, bool selectable)
  184. {
  185. static const Core::String str_option = "option";
  186. if (element->GetTagName() != str_option)
  187. {
  188. Core::Log::Message(Core::Log::LT_WARNING, "A child of '%s' must be of type 'option' but '%s' was given. See element '%s'.", parent_element->GetTagName().c_str(), element->GetTagName().c_str(), parent_element->GetAddress().c_str());
  189. return -1;
  190. }
  191. // Force to block display. Register a click handler so we can be notified of selection.
  192. element->SetProperty(Core::PropertyId::Display, Core::Property(Core::Style::Display::Block));
  193. element->SetProperty(Core::PropertyId::Clip, Core::Property(Core::Style::Clip::Type::Auto));
  194. element->AddEventListener(Core::EventId::Click, this);
  195. int option_index;
  196. if (before < 0 || before >= (int)options.size())
  197. {
  198. Core::Element* ptr = selection_element->AppendChild(std::move(element));
  199. options.push_back(SelectOption(ptr, new_value, selectable));
  200. option_index = (int)options.size() - 1;
  201. }
  202. else
  203. {
  204. Core::Element* ptr = selection_element->InsertBefore(std::move(element), selection_element->GetChild(before));
  205. options.insert(options.begin() + before, SelectOption(ptr, new_value, selectable));
  206. option_index = before;
  207. }
  208. // Select the option if appropriate.
  209. if (select)
  210. SetSelection(option_index);
  211. box_layout_dirty = true;
  212. return option_index;
  213. }
  214. // Removes an option from the select control.
  215. void WidgetDropDown::RemoveOption(int index)
  216. {
  217. if (index < 0 ||
  218. index >= (int) options.size())
  219. return;
  220. // Remove the listener and delete the option element.
  221. options[index].GetElement()->RemoveEventListener(Core::EventId::Click, this);
  222. selection_element->RemoveChild(options[index].GetElement());
  223. options.erase(options.begin() + index);
  224. box_layout_dirty = true;
  225. }
  226. // Removes all options from the list.
  227. void WidgetDropDown::ClearOptions()
  228. {
  229. while (!options.empty())
  230. RemoveOption((int) options.size() - 1);
  231. }
  232. // Returns on of the widget's options.
  233. SelectOption* WidgetDropDown::GetOption(int index)
  234. {
  235. if (index < 0 ||
  236. index >= GetNumOptions())
  237. return nullptr;
  238. return &options[index];
  239. }
  240. // Returns the number of options in the widget.
  241. int WidgetDropDown::GetNumOptions() const
  242. {
  243. return (int) options.size();
  244. }
  245. void WidgetDropDown::ProcessEvent(Core::Event& event)
  246. {
  247. if (parent_element->IsDisabled())
  248. return;
  249. // Process the button onclick
  250. switch (event.GetId())
  251. {
  252. case Core::EventId::Click:
  253. {
  254. if (event.GetCurrentElement()->GetParentNode() == selection_element)
  255. {
  256. // Find the element in the options and fire the selection event
  257. for (size_t i = 0; i < options.size(); i++)
  258. {
  259. if (options[i].GetElement() == event.GetCurrentElement())
  260. {
  261. if (options[i].IsSelectable())
  262. {
  263. SetSelection((int)i);
  264. event.StopPropagation();
  265. ShowSelectBox(false);
  266. parent_element->Focus();
  267. }
  268. }
  269. }
  270. }
  271. else
  272. {
  273. // We have to check that this event isn't targeted to an element
  274. // inside the selection box as we'll get all events coming from our
  275. // root level select element as well as the ones coming from options (which
  276. // get caught in the above if)
  277. Core::Element* element = event.GetTargetElement();
  278. while (element && element != parent_element)
  279. {
  280. if (element == selection_element)
  281. return;
  282. element = element->GetParentNode();
  283. }
  284. if (selection_element->GetComputedValues().visibility == Core::Style::Visibility::Hidden)
  285. ShowSelectBox(true);
  286. else
  287. ShowSelectBox(false);
  288. }
  289. }
  290. break;
  291. case Core::EventId::Focus:
  292. {
  293. if (event.GetTargetElement() == parent_element)
  294. {
  295. value_element->SetPseudoClass("focus", true);
  296. button_element->SetPseudoClass("focus", true);
  297. }
  298. }
  299. break;
  300. case Core::EventId::Blur:
  301. {
  302. if (event.GetTargetElement() == parent_element)
  303. {
  304. ShowSelectBox(false);
  305. value_element->SetPseudoClass("focus", false);
  306. button_element->SetPseudoClass("focus", false);
  307. }
  308. }
  309. break;
  310. case Core::EventId::Keydown:
  311. {
  312. Core::Input::KeyIdentifier key_identifier = (Core::Input::KeyIdentifier) event.GetParameter< int >("key_identifier", 0);
  313. switch (key_identifier)
  314. {
  315. case Core::Input::KI_UP:
  316. SetSelection((selected_option - 1 + (int)options.size()) % (int)options.size());
  317. break;
  318. case Core::Input::KI_DOWN:
  319. SetSelection((selected_option + 1) % (int)options.size());
  320. break;
  321. default:
  322. break;
  323. }
  324. }
  325. break;
  326. default:
  327. break;
  328. }
  329. }
  330. // Shows or hides the selection box.
  331. void WidgetDropDown::ShowSelectBox(bool show)
  332. {
  333. if (show)
  334. {
  335. selection_element->SetProperty(Core::PropertyId::Visibility, Core::Property(Core::Style::Visibility::Visible));
  336. value_element->SetPseudoClass("checked", true);
  337. button_element->SetPseudoClass("checked", true);
  338. }
  339. else
  340. {
  341. selection_element->SetProperty(Core::PropertyId::Visibility, Core::Property(Core::Style::Visibility::Hidden));
  342. value_element->SetPseudoClass("checked", false);
  343. button_element->SetPseudoClass("checked", false);
  344. }
  345. box_visible = show;
  346. }
  347. }
  348. }