WidgetDropDown.cpp 13 KB

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