WidgetDropDown.cpp 13 KB

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