WidgetDropDown.cpp 17 KB

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