WidgetDropDown.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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(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::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& new_value, int before, bool select, bool selectable)
  175. {
  176. Core::ElementPtr element = Core::Factory::InstanceElement(selection_element, "*", "option", Rml::Core::XMLAttributes());
  177. element->SetInnerRML(rml);
  178. bool result = AddOption(std::move(element), new_value, before, select, selectable);
  179. return result;
  180. }
  181. int WidgetDropDown::AddOption(Rml::Core::ElementPtr element, const Rml::Core::String& new_value, int before, bool select, bool selectable)
  182. {
  183. static const Core::String str_option = "option";
  184. if (element->GetTagName() != str_option)
  185. {
  186. 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());
  187. return -1;
  188. }
  189. // Force to block display. Register a click handler so we can be notified of selection.
  190. element->SetProperty(Core::PropertyId::Display, Core::Property(Core::Style::Display::Block));
  191. element->SetProperty(Core::PropertyId::Clip, Core::Property(Core::Style::Clip::Auto));
  192. element->AddEventListener(Core::EventId::Click, this);
  193. int option_index;
  194. if (before < 0 || before >= (int)options.size())
  195. {
  196. Core::Element* ptr = selection_element->AppendChild(std::move(element));
  197. options.push_back(SelectOption(ptr, new_value, selectable));
  198. option_index = (int)options.size() - 1;
  199. }
  200. else
  201. {
  202. Core::Element* ptr = selection_element->InsertBefore(std::move(element), selection_element->GetChild(before));
  203. options.insert(options.begin() + before, SelectOption(ptr, new_value, selectable));
  204. option_index = before;
  205. }
  206. // Select the option if appropriate.
  207. if (select)
  208. SetSelection(option_index);
  209. box_layout_dirty = true;
  210. return option_index;
  211. }
  212. // Removes an option from the select control.
  213. void WidgetDropDown::RemoveOption(int index)
  214. {
  215. if (index < 0 ||
  216. index >= (int) options.size())
  217. return;
  218. // Remove the listener and delete the option element.
  219. options[index].GetElement()->RemoveEventListener(Core::EventId::Click, this);
  220. selection_element->RemoveChild(options[index].GetElement());
  221. options.erase(options.begin() + index);
  222. box_layout_dirty = true;
  223. }
  224. // Removes all options from the list.
  225. void WidgetDropDown::ClearOptions()
  226. {
  227. while (!options.empty())
  228. RemoveOption((int) options.size() - 1);
  229. }
  230. // Returns on of the widget's options.
  231. SelectOption* WidgetDropDown::GetOption(int index)
  232. {
  233. if (index < 0 ||
  234. index >= GetNumOptions())
  235. return nullptr;
  236. return &options[index];
  237. }
  238. // Returns the number of options in the widget.
  239. int WidgetDropDown::GetNumOptions() const
  240. {
  241. return (int) options.size();
  242. }
  243. void WidgetDropDown::ProcessEvent(Core::Event& event)
  244. {
  245. if (parent_element->IsDisabled())
  246. return;
  247. // Process the button onclick
  248. switch (event.GetId())
  249. {
  250. case Core::EventId::Click:
  251. {
  252. if (event.GetCurrentElement()->GetParentNode() == selection_element)
  253. {
  254. // Find the element in the options and fire the selection event
  255. for (size_t i = 0; i < options.size(); i++)
  256. {
  257. if (options[i].GetElement() == event.GetCurrentElement())
  258. {
  259. if (options[i].IsSelectable())
  260. {
  261. SetSelection((int)i);
  262. event.StopPropagation();
  263. ShowSelectBox(false);
  264. parent_element->Focus();
  265. }
  266. }
  267. }
  268. }
  269. else
  270. {
  271. // We have to check that this event isn't targeted to an element
  272. // inside the selection box as we'll get all events coming from our
  273. // root level select element as well as the ones coming from options (which
  274. // get caught in the above if)
  275. Core::Element* element = event.GetTargetElement();
  276. while (element && element != parent_element)
  277. {
  278. if (element == selection_element)
  279. return;
  280. element = element->GetParentNode();
  281. }
  282. if (selection_element->GetComputedValues().visibility == Core::Style::Visibility::Hidden)
  283. ShowSelectBox(true);
  284. else
  285. ShowSelectBox(false);
  286. }
  287. }
  288. break;
  289. case Core::EventId::Focus:
  290. {
  291. if (event.GetTargetElement() == parent_element)
  292. {
  293. value_element->SetPseudoClass("focus", true);
  294. button_element->SetPseudoClass("focus", true);
  295. }
  296. }
  297. case Core::EventId::Blur:
  298. {
  299. if (event.GetTargetElement() == parent_element)
  300. {
  301. ShowSelectBox(false);
  302. value_element->SetPseudoClass("focus", false);
  303. button_element->SetPseudoClass("focus", false);
  304. }
  305. }
  306. break;
  307. case Core::EventId::Keydown:
  308. {
  309. Core::Input::KeyIdentifier key_identifier = (Core::Input::KeyIdentifier) event.GetParameter< int >("key_identifier", 0);
  310. switch (key_identifier)
  311. {
  312. case Core::Input::KI_UP:
  313. SetSelection((selected_option - 1 + (int)options.size()) % (int)options.size());
  314. break;
  315. case Core::Input::KI_DOWN:
  316. SetSelection((selected_option + 1) % (int)options.size());
  317. break;
  318. default:
  319. break;
  320. }
  321. }
  322. break;
  323. default:
  324. break;
  325. }
  326. }
  327. // Shows or hides the selection box.
  328. void WidgetDropDown::ShowSelectBox(bool show)
  329. {
  330. if (show)
  331. {
  332. selection_element->SetProperty(Core::PropertyId::Visibility, Core::Property(Core::Style::Visibility::Visible));
  333. value_element->SetPseudoClass("checked", true);
  334. button_element->SetPseudoClass("checked", true);
  335. }
  336. else
  337. {
  338. selection_element->SetProperty(Core::PropertyId::Visibility, Core::Property(Core::Style::Visibility::Hidden));
  339. value_element->SetPseudoClass("checked", false);
  340. button_element->SetPseudoClass("checked", false);
  341. }
  342. }
  343. }
  344. }