WidgetDropDown.cpp 12 KB

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