WidgetDropDown.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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/Context.h"
  30. #include "../../Include/RmlUi/Core/ElementDocument.h"
  31. #include "../../Include/RmlUi/Core/Math.h"
  32. #include "../../Include/RmlUi/Core/Factory.h"
  33. #include "../../Include/RmlUi/Core/ElementUtilities.h"
  34. #include "../../Include/RmlUi/Core/Event.h"
  35. #include "../../Include/RmlUi/Core/Input.h"
  36. #include "../../Include/RmlUi/Core/Property.h"
  37. #include "../../Include/RmlUi/Core/Profiling.h"
  38. #include "../../Include/RmlUi/Controls/ElementFormControl.h"
  39. namespace Rml {
  40. namespace Controls {
  41. WidgetDropDown::WidgetDropDown(ElementFormControl* element)
  42. {
  43. parent_element = element;
  44. box_layout_dirty = false;
  45. value_layout_dirty = false;
  46. box_visible = false;
  47. selected_option = -1;
  48. // Create the button and selection elements.
  49. button_element = parent_element->AppendChild(Core::Factory::InstanceElement(parent_element, "*", "selectarrow", Rml::Core::XMLAttributes()), false);
  50. value_element = parent_element->AppendChild(Core::Factory::InstanceElement(parent_element, "*", "selectvalue", Rml::Core::XMLAttributes()), false);
  51. selection_element = parent_element->AppendChild(Core::Factory::InstanceElement(parent_element, "*", "selectbox", Rml::Core::XMLAttributes()), false);
  52. value_element->SetProperty(Core::PropertyId::OverflowX, Core::Property(Core::Style::Overflow::Hidden));
  53. value_element->SetProperty(Core::PropertyId::OverflowY, Core::Property(Core::Style::Overflow::Hidden));
  54. selection_element->SetProperty(Core::PropertyId::Visibility, Core::Property(Core::Style::Visibility::Hidden));
  55. selection_element->SetProperty(Core::PropertyId::ZIndex, Core::Property(1.0f, Core::Property::NUMBER));
  56. selection_element->SetProperty(Core::PropertyId::Clip, Core::Property(Core::Style::Clip::Type::None));
  57. selection_element->SetProperty(Core::PropertyId::OverflowY, Core::Property(Core::Style::Overflow::Auto));
  58. parent_element->AddEventListener(Core::EventId::Click, this, true);
  59. parent_element->AddEventListener(Core::EventId::Blur, this);
  60. parent_element->AddEventListener(Core::EventId::Focus, this);
  61. parent_element->AddEventListener(Core::EventId::Keydown, this, true);
  62. selection_element->AddEventListener(Core::EventId::Mousescroll, this);
  63. }
  64. WidgetDropDown::~WidgetDropDown()
  65. {
  66. // We shouldn't clear the options ourselves, as removing the element will automatically clear children.
  67. // However, we do need to remove events of children.
  68. for(auto& option : options)
  69. option.GetElement()->RemoveEventListener(Core::EventId::Click, this);
  70. parent_element->RemoveEventListener(Core::EventId::Click, this, true);
  71. parent_element->RemoveEventListener(Core::EventId::Blur, this);
  72. parent_element->RemoveEventListener(Core::EventId::Focus, this);
  73. parent_element->RemoveEventListener(Core::EventId::Keydown, this, true);
  74. selection_element->RemoveEventListener(Core::EventId::Mousescroll, this);
  75. DetachScrollEvent();
  76. }
  77. // Updates the selection box layout if necessary.
  78. void WidgetDropDown::OnRender()
  79. {
  80. if (box_visible && box_layout_dirty)
  81. {
  82. // Layout the selection box.
  83. // The following procedure should ensure that the selection box is never (partly) outside of the context's window.
  84. // This is achieved by positioning the box either above or below the 'select' element, and possibly shrinking
  85. // the element's height.
  86. // We try to respect user values of 'height', 'min-height', and 'max-height'. However, when we need to shrink the box
  87. // we will override the 'height' property.
  88. using Rml::Core::Vector2f;
  89. // Previously set 'height' property from this procedure must be removed for the calculations below to work as intended.
  90. if(selection_element->GetLocalStyleProperties().count(Core::PropertyId::Height) == 1)
  91. {
  92. selection_element->RemoveProperty(Core::PropertyId::Height);
  93. selection_element->GetOwnerDocument()->UpdateDocument();
  94. }
  95. Core::Box box;
  96. Core::ElementUtilities::BuildBox(box, parent_element->GetBox().GetSize(), selection_element);
  97. // The user can use 'margin-left/top/bottom' to offset the box away from the 'select' element, respectively
  98. // horizontally, vertically when box below, and vertically when box above.
  99. const float offset_x = box.GetEdge(Core::Box::MARGIN, Core::Box::LEFT);
  100. const float offset_y_below = parent_element->GetBox().GetSize(Core::Box::BORDER).y + box.GetEdge(Core::Box::MARGIN, Core::Box::TOP);
  101. const float offset_y_above = -box.GetEdge(Core::Box::MARGIN, Core::Box::BOTTOM);
  102. float window_height = 100'000.f;
  103. if (Core::Context* context = parent_element->GetContext())
  104. window_height = float(context->GetDimensions().y);
  105. const float absolute_y = parent_element->GetAbsoluteOffset(Rml::Core::Box::BORDER).y;
  106. const float height_below = window_height - absolute_y - offset_y_below;
  107. const float height_above = absolute_y + offset_y_above;
  108. // Format the selection box and retrieve the 'native' height occupied by all the options, while respecting
  109. // the 'min/max-height' properties.
  110. Core::ElementUtilities::FormatElement(selection_element, parent_element->GetBox().GetSize(Core::Box::BORDER));
  111. const float content_height = selection_element->GetOffsetHeight();
  112. if (content_height < height_below)
  113. {
  114. // Position box below
  115. selection_element->SetOffset(Vector2f(offset_x, offset_y_below), parent_element);
  116. }
  117. else if (content_height < height_above)
  118. {
  119. // Position box above
  120. selection_element->SetOffset(Vector2f(offset_x, -content_height + offset_y_above), parent_element);
  121. }
  122. else
  123. {
  124. // Shrink box and position either below or above
  125. const float padding_border_size =
  126. box.GetEdge(Core::Box::BORDER, Core::Box::TOP) + box.GetEdge(Core::Box::BORDER, Core::Box::BOTTOM) +
  127. box.GetEdge(Core::Box::PADDING, Core::Box::TOP) + box.GetEdge(Core::Box::PADDING, Core::Box::BOTTOM);
  128. float height = 0.f;
  129. float offset_y = 0.f;
  130. if (height_below > height_above)
  131. {
  132. // Position below
  133. height = height_below - padding_border_size;
  134. offset_y = offset_y_below;
  135. }
  136. else
  137. {
  138. // Position above
  139. height = height_above - padding_border_size;
  140. offset_y = offset_y_above - height_above;
  141. }
  142. // Set the height and re-format the selection box.
  143. selection_element->SetProperty(Core::PropertyId::Height, Core::Property(height, Core::Property::PX));
  144. selection_element->GetOwnerDocument()->UpdateDocument();
  145. Core::ElementUtilities::FormatElement(selection_element, parent_element->GetBox().GetSize(Core::Box::BORDER));
  146. selection_element->SetOffset(Vector2f(offset_x, offset_y), parent_element);
  147. }
  148. box_layout_dirty = false;
  149. }
  150. if (value_layout_dirty)
  151. {
  152. Core::ElementUtilities::FormatElement(value_element, parent_element->GetBox().GetSize(Core::Box::BORDER));
  153. value_element->SetOffset(parent_element->GetBox().GetPosition(Core::Box::CONTENT), parent_element);
  154. value_layout_dirty = false;
  155. }
  156. }
  157. void WidgetDropDown::OnLayout()
  158. {
  159. RMLUI_ZoneScopedNC("DropDownLayout", 0x7FFF00);
  160. if(parent_element->IsDisabled())
  161. {
  162. // Propagate disabled state to selectvalue and selectarrow
  163. value_element->SetPseudoClass("disabled", true);
  164. button_element->SetPseudoClass("disabled", true);
  165. }
  166. // Layout the button and selection boxes.
  167. Core::Box parent_box = parent_element->GetBox();
  168. Core::ElementUtilities::PositionElement(button_element, Rml::Core::Vector2f(0, 0), Core::ElementUtilities::TOP_RIGHT);
  169. Core::ElementUtilities::PositionElement(selection_element, Rml::Core::Vector2f(0, 0), Core::ElementUtilities::TOP_LEFT);
  170. // Calculate the value element position and size.
  171. Rml::Core::Vector2f size;
  172. size.x = parent_element->GetBox().GetSize(Core::Box::CONTENT).x - button_element->GetBox().GetSize(Core::Box::MARGIN).x;
  173. size.y = parent_element->GetBox().GetSize(Core::Box::CONTENT).y;
  174. value_element->SetOffset(parent_element->GetBox().GetPosition(Core::Box::CONTENT), parent_element);
  175. value_element->SetBox(Core::Box(size));
  176. box_layout_dirty = true;
  177. value_layout_dirty = true;
  178. }
  179. // Sets the value of the widget.
  180. void WidgetDropDown::SetValue(const Rml::Core::String& _value)
  181. {
  182. for (size_t i = 0; i < options.size(); ++i)
  183. {
  184. if (options[i].GetValue() == _value)
  185. {
  186. SetSelection((int) i);
  187. return;
  188. }
  189. }
  190. if (selected_option >= 0 && selected_option < (int)options.size())
  191. options[selected_option].GetElement()->SetPseudoClass("checked", false);
  192. value = _value;
  193. value_element->SetInnerRML(value);
  194. value_layout_dirty = true;
  195. selected_option = -1;
  196. }
  197. // Returns the current value of the widget.
  198. const Rml::Core::String& WidgetDropDown::GetValue() const
  199. {
  200. return value;
  201. }
  202. // Sets the index of the selection. If the new index lies outside of the bounds, it will be clamped.
  203. void WidgetDropDown::SetSelection(int selection, bool force)
  204. {
  205. Rml::Core::String new_value;
  206. if (selection < 0 ||
  207. selection >= (int) options.size())
  208. {
  209. selection = -1;
  210. }
  211. else
  212. {
  213. new_value = options[selection].GetValue();
  214. }
  215. if (force ||
  216. selection != selected_option ||
  217. value != new_value)
  218. {
  219. if (selected_option >= 0 && selected_option < (int)options.size())
  220. options[selected_option].GetElement()->SetPseudoClass("checked", false);
  221. selected_option = selection;
  222. value = new_value;
  223. Rml::Core::String value_rml;
  224. if (selected_option >= 0)
  225. {
  226. auto* el = options[selected_option].GetElement();
  227. el->GetInnerRML(value_rml);
  228. el->SetPseudoClass("checked", true);
  229. }
  230. value_element->SetInnerRML(value_rml);
  231. value_layout_dirty = true;
  232. Rml::Core::Dictionary parameters;
  233. parameters["value"] = value;
  234. parent_element->DispatchEvent(Core::EventId::Change, parameters);
  235. }
  236. }
  237. // Returns the index of the currently selected item.
  238. int WidgetDropDown::GetSelection() const
  239. {
  240. return selected_option;
  241. }
  242. // Adds a new option to the select control.
  243. int WidgetDropDown::AddOption(const Rml::Core::String& rml, const Rml::Core::String& new_value, int before, bool select, bool selectable)
  244. {
  245. Core::ElementPtr element = Core::Factory::InstanceElement(selection_element, "*", "option", Rml::Core::XMLAttributes());
  246. element->SetInnerRML(rml);
  247. int result = AddOption(std::move(element), new_value, before, select, selectable);
  248. return result;
  249. }
  250. int WidgetDropDown::AddOption(Rml::Core::ElementPtr element, const Rml::Core::String& new_value, int before, bool select, bool selectable)
  251. {
  252. static const Core::String str_option = "option";
  253. if (element->GetTagName() != str_option)
  254. {
  255. 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());
  256. return -1;
  257. }
  258. // Force to block display. Register a click handler so we can be notified of selection.
  259. element->SetProperty(Core::PropertyId::Display, Core::Property(Core::Style::Display::Block));
  260. element->SetProperty(Core::PropertyId::Clip, Core::Property(Core::Style::Clip::Type::Auto));
  261. element->AddEventListener(Core::EventId::Click, this);
  262. int option_index;
  263. if (before < 0 || before >= (int)options.size())
  264. {
  265. Core::Element* ptr = selection_element->AppendChild(std::move(element));
  266. options.push_back(SelectOption(ptr, new_value, selectable));
  267. option_index = (int)options.size() - 1;
  268. }
  269. else
  270. {
  271. Core::Element* ptr = selection_element->InsertBefore(std::move(element), selection_element->GetChild(before));
  272. options.insert(options.begin() + before, SelectOption(ptr, new_value, selectable));
  273. option_index = before;
  274. }
  275. // Select the option if appropriate.
  276. if (select)
  277. SetSelection(option_index);
  278. box_layout_dirty = true;
  279. return option_index;
  280. }
  281. // Removes an option from the select control.
  282. void WidgetDropDown::RemoveOption(int index)
  283. {
  284. if (index < 0 ||
  285. index >= (int) options.size())
  286. return;
  287. // Remove the listener and delete the option element.
  288. options[index].GetElement()->RemoveEventListener(Core::EventId::Click, this);
  289. selection_element->RemoveChild(options[index].GetElement());
  290. options.erase(options.begin() + index);
  291. box_layout_dirty = true;
  292. }
  293. // Removes all options from the list.
  294. void WidgetDropDown::ClearOptions()
  295. {
  296. while (!options.empty())
  297. RemoveOption((int) options.size() - 1);
  298. }
  299. // Returns on of the widget's options.
  300. SelectOption* WidgetDropDown::GetOption(int index)
  301. {
  302. if (index < 0 ||
  303. index >= GetNumOptions())
  304. return nullptr;
  305. return &options[index];
  306. }
  307. // Returns the number of options in the widget.
  308. int WidgetDropDown::GetNumOptions() const
  309. {
  310. return (int) options.size();
  311. }
  312. void WidgetDropDown::AttachScrollEvent()
  313. {
  314. if (Core::ElementDocument* document = parent_element->GetOwnerDocument())
  315. document->AddEventListener(Core::EventId::Scroll, this, true);
  316. }
  317. void WidgetDropDown::DetachScrollEvent()
  318. {
  319. if (Core::ElementDocument* document = parent_element->GetOwnerDocument())
  320. document->RemoveEventListener(Core::EventId::Scroll, this, true);
  321. }
  322. void WidgetDropDown::ProcessEvent(Core::Event& event)
  323. {
  324. if (parent_element->IsDisabled())
  325. return;
  326. // Process the button onclick
  327. switch (event.GetId())
  328. {
  329. case Core::EventId::Click:
  330. {
  331. if (event.GetCurrentElement()->GetParentNode() == selection_element)
  332. {
  333. // Find the element in the options and fire the selection event
  334. for (size_t i = 0; i < options.size(); i++)
  335. {
  336. if (options[i].GetElement() == event.GetCurrentElement())
  337. {
  338. if (options[i].IsSelectable())
  339. {
  340. SetSelection((int)i);
  341. event.StopPropagation();
  342. ShowSelectBox(false);
  343. parent_element->Focus();
  344. }
  345. }
  346. }
  347. }
  348. else
  349. {
  350. // We have to check that this event isn't targeted to an element
  351. // inside the selection box as we'll get all events coming from our
  352. // root level select element as well as the ones coming from options (which
  353. // get caught in the above if)
  354. Core::Element* element = event.GetTargetElement();
  355. while (element && element != parent_element)
  356. {
  357. if (element == selection_element)
  358. return;
  359. element = element->GetParentNode();
  360. }
  361. if (selection_element->GetComputedValues().visibility == Core::Style::Visibility::Hidden)
  362. ShowSelectBox(true);
  363. else
  364. ShowSelectBox(false);
  365. }
  366. }
  367. break;
  368. case Core::EventId::Focus:
  369. {
  370. if (event.GetTargetElement() == parent_element)
  371. {
  372. value_element->SetPseudoClass("focus", true);
  373. button_element->SetPseudoClass("focus", true);
  374. }
  375. }
  376. break;
  377. case Core::EventId::Blur:
  378. {
  379. if (event.GetTargetElement() == parent_element)
  380. {
  381. ShowSelectBox(false);
  382. value_element->SetPseudoClass("focus", false);
  383. button_element->SetPseudoClass("focus", false);
  384. }
  385. }
  386. break;
  387. case Core::EventId::Keydown:
  388. {
  389. Core::Input::KeyIdentifier key_identifier = (Core::Input::KeyIdentifier) event.GetParameter< int >("key_identifier", 0);
  390. switch (key_identifier)
  391. {
  392. case Core::Input::KI_UP:
  393. SetSelection((selected_option - 1 + (int)options.size()) % (int)options.size());
  394. break;
  395. case Core::Input::KI_DOWN:
  396. SetSelection((selected_option + 1) % (int)options.size());
  397. break;
  398. default:
  399. break;
  400. }
  401. }
  402. break;
  403. case Core::EventId::Mousescroll:
  404. {
  405. if (event.GetCurrentElement() == selection_element)
  406. {
  407. // Prevent scrolling in the parent window when mouse is inside the selection box.
  408. event.StopPropagation();
  409. // Stopping propagation also stops all default scrolling actions. However, we still want to be able
  410. // to scroll in the selection box, so call the default action manually.
  411. selection_element->ProcessDefaultAction(event);
  412. }
  413. }
  414. break;
  415. case Core::EventId::Scroll:
  416. {
  417. if (box_visible)
  418. {
  419. // Close the select box if we scroll outside the select box.
  420. bool scrolls_selection_box = false;
  421. for (Core::Element* element = event.GetTargetElement(); element; element = element->GetParentNode())
  422. {
  423. if (element == selection_element)
  424. {
  425. scrolls_selection_box = true;
  426. break;
  427. }
  428. }
  429. if (!scrolls_selection_box)
  430. ShowSelectBox(false);
  431. }
  432. }
  433. break;
  434. default:
  435. break;
  436. }
  437. }
  438. // Shows or hides the selection box.
  439. void WidgetDropDown::ShowSelectBox(bool show)
  440. {
  441. if (show)
  442. {
  443. selection_element->SetProperty(Core::PropertyId::Visibility, Core::Property(Core::Style::Visibility::Visible));
  444. value_element->SetPseudoClass("checked", true);
  445. button_element->SetPseudoClass("checked", true);
  446. box_layout_dirty = true;
  447. AttachScrollEvent();
  448. }
  449. else
  450. {
  451. selection_element->SetProperty(Core::PropertyId::Visibility, Core::Property(Core::Style::Visibility::Hidden));
  452. selection_element->RemoveProperty(Core::PropertyId::Height);
  453. value_element->SetPseudoClass("checked", false);
  454. button_element->SetPseudoClass("checked", false);
  455. DetachScrollEvent();
  456. }
  457. box_visible = show;
  458. }
  459. }
  460. }