WidgetDropDown.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  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-2023 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/ComputedValues.h"
  30. #include "../../../Include/RmlUi/Core/Context.h"
  31. #include "../../../Include/RmlUi/Core/ElementDocument.h"
  32. #include "../../../Include/RmlUi/Core/ElementUtilities.h"
  33. #include "../../../Include/RmlUi/Core/Elements/ElementFormControl.h"
  34. #include "../../../Include/RmlUi/Core/Event.h"
  35. #include "../../../Include/RmlUi/Core/Factory.h"
  36. #include "../../../Include/RmlUi/Core/Input.h"
  37. #include "../../../Include/RmlUi/Core/Math.h"
  38. #include "../../../Include/RmlUi/Core/Profiling.h"
  39. #include "../../../Include/RmlUi/Core/Property.h"
  40. #include "../DataModel.h"
  41. namespace Rml {
  42. WidgetDropDown::WidgetDropDown(ElementFormControl* element)
  43. {
  44. parent_element = element;
  45. // Create the button and selection elements.
  46. button_element = parent_element->AppendChild(Factory::InstanceElement(parent_element, "*", "selectarrow", XMLAttributes()), false);
  47. value_element = parent_element->AppendChild(Factory::InstanceElement(parent_element, "*", "selectvalue", XMLAttributes()), false);
  48. selection_element = parent_element->AppendChild(Factory::InstanceElement(parent_element, "*", "selectbox", XMLAttributes()), false);
  49. value_element->SetProperty(PropertyId::OverflowX, Property(Style::Overflow::Hidden));
  50. value_element->SetProperty(PropertyId::OverflowY, Property(Style::Overflow::Hidden));
  51. selection_element->SetProperty(PropertyId::Visibility, Property(Style::Visibility::Hidden));
  52. selection_element->SetProperty(PropertyId::ZIndex, Property(1.0f, Unit::NUMBER));
  53. selection_element->SetProperty(PropertyId::Clip, Property(Style::Clip::Type::None));
  54. selection_element->SetProperty(PropertyId::OverflowY, Property(Style::Overflow::Auto));
  55. // Prevent scrolling in the parent document when the mouse is inside the selection box.
  56. selection_element->SetProperty(PropertyId::OverscrollBehavior, Property(Style::OverscrollBehavior::Contain));
  57. parent_element->AddEventListener(EventId::Click, this, true);
  58. parent_element->AddEventListener(EventId::Blur, this);
  59. parent_element->AddEventListener(EventId::Focus, this);
  60. parent_element->AddEventListener(EventId::Keydown, this, true);
  61. }
  62. WidgetDropDown::~WidgetDropDown()
  63. {
  64. // We shouldn't clear the options ourselves, as removing the element will automatically clear children.
  65. // However, we do need to remove events of children.
  66. const int num_options = selection_element->GetNumChildren();
  67. for (int i = 0; i < num_options; i++)
  68. selection_element->GetChild(i)->RemoveEventListener(EventId::Click, this);
  69. parent_element->RemoveEventListener(EventId::Click, this, true);
  70. parent_element->RemoveEventListener(EventId::Blur, this);
  71. parent_element->RemoveEventListener(EventId::Focus, this);
  72. parent_element->RemoveEventListener(EventId::Keydown, this, true);
  73. DetachScrollEvent();
  74. }
  75. void WidgetDropDown::OnUpdate()
  76. {
  77. if (selection_dirty)
  78. {
  79. // Find the best option element to select in the following priority:
  80. // 1. First option with 'selected' attribute.
  81. // 2. An option whose 'value' attribute matches the select 'value' attribute.
  82. // 3. The first option.
  83. // The select element's value may change as a result of this.
  84. const String select_value = parent_element->GetAttribute("value", String());
  85. Element* select_option = selection_element->GetFirstChild();
  86. const int num_options = selection_element->GetNumChildren();
  87. for (int i = 0; i < num_options; i++)
  88. {
  89. Element* option = selection_element->GetChild(i);
  90. if (option->HasAttribute("selected"))
  91. {
  92. select_option = option;
  93. break;
  94. }
  95. else if (!select_value.empty() && select_value == option->GetAttribute("value", String()))
  96. {
  97. select_option = option;
  98. }
  99. }
  100. if (select_option)
  101. SetSelection(select_option);
  102. selection_dirty = false;
  103. }
  104. if (value_rml_dirty)
  105. {
  106. String value_rml;
  107. const int selection = GetSelection();
  108. if (Element* option = selection_element->GetChild(selection))
  109. {
  110. option->GetInnerRML(value_rml);
  111. if (auto model = value_element->GetDataModel())
  112. model->CopyAliases(option, value_element);
  113. }
  114. else
  115. {
  116. if (auto model = value_element->GetDataModel())
  117. model->EraseAliases(value_element);
  118. value_rml = parent_element->GetValue();
  119. }
  120. value_element->SetInnerRML(value_rml);
  121. value_rml_dirty = false;
  122. value_layout_dirty = true;
  123. }
  124. }
  125. void WidgetDropDown::OnRender()
  126. {
  127. if (box_visible && box_layout_dirty)
  128. {
  129. // Layout the selection box.
  130. // The following procedure should ensure that the selection box is never (partly) outside of the context's window.
  131. // This is achieved by positioning the box either above or below the 'select' element, and possibly shrinking
  132. // the element's height.
  133. // We try to respect user values of 'height', 'min-height', and 'max-height'. However, when we need to shrink the box
  134. // we will override the 'height' property.
  135. const float initial_used_height = selection_element->GetBox().GetSize().y;
  136. const Vector2f initial_scroll_offset = {selection_element->GetScrollLeft(), selection_element->GetScrollTop()};
  137. // Previously set 'height' property from this procedure must be removed for the calculations below to work as intended.
  138. if (selection_element->GetLocalStyleProperties().count(PropertyId::Height) == 1)
  139. {
  140. selection_element->RemoveProperty(PropertyId::Height);
  141. selection_element->GetOwnerDocument()->UpdateDocument();
  142. }
  143. Box box;
  144. ElementUtilities::BuildBox(box, parent_element->GetBox().GetSize(), selection_element);
  145. // The user can use 'margin-left/top/bottom' to offset the box away from the 'select' element, respectively
  146. // horizontally, vertically when box below, and vertically when box above.
  147. const float offset_x = box.GetEdge(BoxArea::Margin, BoxEdge::Left);
  148. const float offset_y_below = parent_element->GetBox().GetSize(BoxArea::Border).y + box.GetEdge(BoxArea::Margin, BoxEdge::Top);
  149. const float offset_y_above = -box.GetEdge(BoxArea::Margin, BoxEdge::Bottom);
  150. float window_height = 100'000.f;
  151. if (Context* context = parent_element->GetContext())
  152. window_height = float(context->GetDimensions().y);
  153. const float absolute_y = parent_element->GetAbsoluteOffset(BoxArea::Border).y;
  154. const float height_below = window_height - absolute_y - offset_y_below;
  155. const float height_above = absolute_y + offset_y_above;
  156. // Format the selection box and retrieve the 'native' height occupied by all the options, while respecting
  157. // the 'min/max-height' properties.
  158. ElementUtilities::FormatElement(selection_element, parent_element->GetBox().GetSize(BoxArea::Border));
  159. const float content_height = selection_element->GetOffsetHeight();
  160. if (content_height < height_below)
  161. {
  162. // Position box below
  163. selection_element->SetOffset(Vector2f(offset_x, offset_y_below), parent_element);
  164. }
  165. else if (content_height < height_above)
  166. {
  167. // Position box above
  168. selection_element->SetOffset(Vector2f(offset_x, -content_height + offset_y_above), parent_element);
  169. }
  170. else
  171. {
  172. // Shrink box and position either below or above
  173. const float padding_border_size = box.GetSizeAcross(BoxDirection::Vertical, BoxArea::Border, BoxArea::Padding);
  174. float height = 0.f;
  175. float offset_y = 0.f;
  176. if (height_below > height_above)
  177. {
  178. // Position below
  179. height = height_below - padding_border_size;
  180. offset_y = offset_y_below;
  181. }
  182. else
  183. {
  184. // Position above
  185. height = height_above - padding_border_size;
  186. offset_y = offset_y_above - height_above;
  187. }
  188. // Set the height and re-format the selection box.
  189. // @performance: This causes a re-layout of the document since we're setting the height and then updating
  190. // the document. However, the re-layout is not really needed, since the document's layout is independent of
  191. // the selection element's size, and we do all the formatting for the element here. We really only call
  192. // `UpdateDocument` to update the properties. See also `RemoveProperty` for height above.
  193. selection_element->SetProperty(PropertyId::Height, Property(height, Unit::PX));
  194. selection_element->GetOwnerDocument()->UpdateDocument();
  195. ElementUtilities::FormatElement(selection_element, parent_element->GetBox().GetSize(BoxArea::Border));
  196. // Set the scroll offset back, since it may have been clamped during the first element formatting.
  197. selection_element->SetScrollLeft(initial_scroll_offset.x);
  198. selection_element->SetScrollTop(initial_scroll_offset.y);
  199. selection_element->SetOffset(Vector2f(offset_x, offset_y), parent_element);
  200. }
  201. const float new_used_height = selection_element->GetBox().GetSize().y;
  202. const bool should_scroll_into_view =
  203. (box_opened_since_last_format || value_changed_since_last_box_format || initial_used_height != new_used_height);
  204. const int selection = GetSelection();
  205. if (should_scroll_into_view && selection != -1)
  206. {
  207. ScrollIntoViewOptions scroll_options = {
  208. box_opened_since_last_format ? ScrollAlignment::Center : ScrollAlignment::Nearest,
  209. ScrollAlignment::Nearest,
  210. ScrollBehavior::Instant,
  211. ScrollParentage::Closest,
  212. };
  213. GetOption(selection)->ScrollIntoView(scroll_options);
  214. }
  215. box_opened_since_last_format = false;
  216. value_changed_since_last_box_format = false;
  217. box_layout_dirty = false;
  218. }
  219. if (value_layout_dirty)
  220. {
  221. ElementUtilities::FormatElement(value_element, parent_element->GetBox().GetSize(BoxArea::Border));
  222. value_element->SetOffset(parent_element->GetBox().GetPosition(BoxArea::Content), parent_element);
  223. value_layout_dirty = false;
  224. }
  225. }
  226. void WidgetDropDown::OnLayout()
  227. {
  228. RMLUI_ZoneScopedNC("DropDownLayout", 0x7FFF00);
  229. if (parent_element->IsDisabled())
  230. {
  231. // Propagate disabled state to selectvalue and selectarrow
  232. value_element->SetPseudoClass("disabled", true);
  233. button_element->SetPseudoClass("disabled", true);
  234. }
  235. // Layout the button box. The selection element layout is deferred until it is opened.
  236. ElementUtilities::PositionElement(button_element, Vector2f(0, 0), ElementUtilities::TOP_RIGHT);
  237. // Calculate the value element position and size.
  238. Vector2f size;
  239. size.x = parent_element->GetBox().GetSize(BoxArea::Content).x - button_element->GetBox().GetSize(BoxArea::Margin).x;
  240. size.y = parent_element->GetBox().GetSize(BoxArea::Content).y;
  241. value_element->SetOffset(parent_element->GetBox().GetPosition(BoxArea::Content), parent_element);
  242. value_element->SetBox(Box(size));
  243. box_layout_dirty = true;
  244. value_layout_dirty = true;
  245. }
  246. void WidgetDropDown::OnValueChange(const String& value)
  247. {
  248. if (!lock_selection)
  249. {
  250. Element* select_option = nullptr;
  251. const int num_options = selection_element->GetNumChildren();
  252. for (int i = 0; i < num_options; i++)
  253. {
  254. Element* option = selection_element->GetChild(i);
  255. Variant* variant = option->GetAttribute("value");
  256. if (variant && variant->Get<String>() == value)
  257. {
  258. select_option = option;
  259. break;
  260. }
  261. }
  262. if (select_option && !select_option->HasAttribute("selected"))
  263. SetSelection(select_option);
  264. }
  265. Dictionary parameters;
  266. parameters["value"] = value;
  267. parent_element->DispatchEvent(EventId::Change, parameters);
  268. value_rml_dirty = true;
  269. value_changed_since_last_box_format = true;
  270. }
  271. void WidgetDropDown::SetSelection(Element* select_option, bool force)
  272. {
  273. const String old_value = parent_element->GetAttribute("value", String());
  274. const String new_value = select_option ? select_option->GetAttribute("value", String()) : String();
  275. bool newly_selected = false;
  276. const int num_options = selection_element->GetNumChildren();
  277. for (int i = 0; i < num_options; i++)
  278. {
  279. Element* option = selection_element->GetChild(i);
  280. if (select_option == option)
  281. {
  282. if (!option->IsPseudoClassSet("checked"))
  283. newly_selected = true;
  284. option->SetAttribute("selected", String());
  285. option->SetPseudoClass("checked", true);
  286. }
  287. else
  288. {
  289. option->RemoveAttribute("selected");
  290. option->SetPseudoClass("checked", false);
  291. }
  292. }
  293. if (force || newly_selected || (old_value != new_value))
  294. {
  295. lock_selection = true;
  296. parent_element->SetAttribute("value", new_value);
  297. lock_selection = false;
  298. }
  299. value_rml_dirty = true;
  300. }
  301. void WidgetDropDown::SeekSelection(bool seek_forward)
  302. {
  303. const int selected_option = GetSelection();
  304. const int num_options = selection_element->GetNumChildren();
  305. const int seek_direction = (seek_forward ? 1 : -1);
  306. for (int i = selected_option + seek_direction; i >= 0 && i < num_options; i += seek_direction)
  307. {
  308. Element* element = selection_element->GetChild(i);
  309. if (!element->HasAttribute("disabled") && element->IsVisible())
  310. {
  311. SetSelection(element);
  312. return;
  313. }
  314. }
  315. // No valid option found, leave selection unchanged.
  316. }
  317. int WidgetDropDown::GetSelection() const
  318. {
  319. const int num_options = selection_element->GetNumChildren();
  320. for (int i = 0; i < num_options; i++)
  321. {
  322. if (selection_element->GetChild(i)->HasAttribute("selected"))
  323. return i;
  324. }
  325. return -1;
  326. }
  327. int WidgetDropDown::AddOption(const String& rml, const String& option_value, int before, bool select, bool selectable)
  328. {
  329. ElementPtr element = Factory::InstanceElement(selection_element, "*", "option", XMLAttributes());
  330. element->SetInnerRML(rml);
  331. element->SetAttribute("value", option_value);
  332. if (select)
  333. element->SetAttribute("selected", String());
  334. if (!selectable)
  335. element->SetAttribute("disabled", String());
  336. int result = AddOption(std::move(element), before);
  337. return result;
  338. }
  339. int WidgetDropDown::AddOption(ElementPtr element, int before)
  340. {
  341. if (element->GetTagName() != "option")
  342. {
  343. Log::Message(Log::LT_WARNING, "A child of '%s' must be of type 'option' but '%s' was given. See element '%s'.",
  344. parent_element->GetTagName().c_str(), element->GetTagName().c_str(), parent_element->GetAddress().c_str());
  345. return -1;
  346. }
  347. const int num_children_before = selection_element->GetNumChildren();
  348. int option_index;
  349. if (before < 0 || before >= num_children_before)
  350. {
  351. selection_element->AppendChild(std::move(element));
  352. option_index = num_children_before;
  353. }
  354. else
  355. {
  356. selection_element->InsertBefore(std::move(element), selection_element->GetChild(before));
  357. option_index = before;
  358. }
  359. return option_index;
  360. }
  361. void WidgetDropDown::RemoveOption(int index)
  362. {
  363. Element* element = selection_element->GetChild(index);
  364. if (!element)
  365. return;
  366. selection_element->RemoveChild(element);
  367. }
  368. void WidgetDropDown::ClearOptions()
  369. {
  370. while (Element* element = selection_element->GetLastChild())
  371. selection_element->RemoveChild(element);
  372. }
  373. Element* WidgetDropDown::GetOption(int index)
  374. {
  375. return selection_element->GetChild(index);
  376. }
  377. int WidgetDropDown::GetNumOptions() const
  378. {
  379. return selection_element->GetNumChildren();
  380. }
  381. void WidgetDropDown::OnChildAdd(Element* element)
  382. {
  383. // We have a special case for 'data-for' here, since that element must remain hidden.
  384. if (element->GetParentNode() != selection_element || element->HasAttribute("data-for") || element->GetTagName() != "option")
  385. return;
  386. // Force to block display. Register a click handler so we can be notified of selection.
  387. element->SetProperty(PropertyId::Display, Property(Style::Display::Block));
  388. element->SetProperty(PropertyId::Clip, Property(Style::Clip::Type::Auto));
  389. element->AddEventListener(EventId::Click, this);
  390. // Select the option if appropriate.
  391. if (element->HasAttribute("selected"))
  392. SetSelection(element, true);
  393. selection_dirty = true;
  394. box_layout_dirty = true;
  395. }
  396. void WidgetDropDown::OnChildRemove(Element* element)
  397. {
  398. if (element->GetParentNode() != selection_element)
  399. return;
  400. element->RemoveEventListener(EventId::Click, this);
  401. if (element->HasAttribute("selected"))
  402. SetSelection(nullptr);
  403. selection_dirty = true;
  404. box_layout_dirty = true;
  405. }
  406. void WidgetDropDown::AttachScrollEvent()
  407. {
  408. if (ElementDocument* document = parent_element->GetOwnerDocument())
  409. document->AddEventListener(EventId::Scroll, this, true);
  410. }
  411. void WidgetDropDown::DetachScrollEvent()
  412. {
  413. if (ElementDocument* document = parent_element->GetOwnerDocument())
  414. document->RemoveEventListener(EventId::Scroll, this, true);
  415. }
  416. void WidgetDropDown::ProcessEvent(Event& event)
  417. {
  418. if (parent_element->IsDisabled())
  419. return;
  420. // Process the button onclick
  421. switch (event.GetId())
  422. {
  423. case EventId::Click:
  424. {
  425. if (event.GetCurrentElement()->GetParentNode() == selection_element)
  426. {
  427. const int num_options = selection_element->GetNumChildren();
  428. // Find the element in the options and fire the selection event
  429. for (int i = 0; i < num_options; i++)
  430. {
  431. Element* current_element = event.GetCurrentElement();
  432. if (selection_element->GetChild(i) == current_element)
  433. {
  434. if (!event.GetCurrentElement()->HasAttribute("disabled"))
  435. {
  436. SetSelection(current_element);
  437. event.StopPropagation();
  438. HideSelectBox();
  439. parent_element->Focus();
  440. }
  441. }
  442. }
  443. }
  444. else
  445. {
  446. // We have to check that this event isn't targeted to an element
  447. // inside the selection box as we'll get all events coming from our
  448. // root level select element as well as the ones coming from options (which
  449. // get caught in the above if)
  450. Element* element = event.GetTargetElement();
  451. while (element && element != parent_element)
  452. {
  453. if (element == selection_element)
  454. return;
  455. element = element->GetParentNode();
  456. }
  457. if (selection_element->GetComputedValues().visibility() == Style::Visibility::Hidden)
  458. ShowSelectBox();
  459. else
  460. HideSelectBox();
  461. }
  462. }
  463. break;
  464. case EventId::Focus:
  465. {
  466. if (event.GetTargetElement() == parent_element)
  467. {
  468. value_element->SetPseudoClass("focus", true);
  469. button_element->SetPseudoClass("focus", true);
  470. }
  471. }
  472. break;
  473. case EventId::Blur:
  474. {
  475. if (event.GetTargetElement() == parent_element)
  476. {
  477. HideSelectBox();
  478. value_element->SetPseudoClass("focus", false);
  479. button_element->SetPseudoClass("focus", false);
  480. }
  481. }
  482. break;
  483. case EventId::Keydown:
  484. {
  485. Input::KeyIdentifier key_identifier = (Input::KeyIdentifier)event.GetParameter<int>("key_identifier", 0);
  486. auto HasVerticalNavigation = [this](PropertyId id) {
  487. if (const Property* p = parent_element->GetProperty(id))
  488. {
  489. if (p->unit != Unit::KEYWORD)
  490. return true;
  491. const Style::Nav nav = static_cast<Style::Nav>(p->Get<int>());
  492. if (nav == Style::Nav::Auto || nav == Style::Nav::Vertical)
  493. return true;
  494. }
  495. return false;
  496. };
  497. switch (key_identifier)
  498. {
  499. case Input::KI_UP:
  500. if (!box_visible && HasVerticalNavigation(PropertyId::NavUp))
  501. break;
  502. SeekSelection(false);
  503. event.StopPropagation();
  504. break;
  505. case Input::KI_DOWN:
  506. if (!box_visible && HasVerticalNavigation(PropertyId::NavDown))
  507. break;
  508. SeekSelection(true);
  509. event.StopPropagation();
  510. break;
  511. case Input::KI_RETURN:
  512. case Input::KI_NUMPADENTER:
  513. parent_element->Click();
  514. event.StopPropagation();
  515. break;
  516. default: break;
  517. }
  518. }
  519. break;
  520. case EventId::Scroll:
  521. {
  522. if (box_visible)
  523. {
  524. // Close the select box if we scroll outside the select box.
  525. bool scrolls_selection_box = false;
  526. for (Element* element = event.GetTargetElement(); element; element = element->GetParentNode())
  527. {
  528. if (element == selection_element)
  529. {
  530. scrolls_selection_box = true;
  531. break;
  532. }
  533. }
  534. if (!scrolls_selection_box)
  535. HideSelectBox();
  536. }
  537. }
  538. break;
  539. default: break;
  540. }
  541. }
  542. void WidgetDropDown::ShowSelectBox()
  543. {
  544. if (box_visible)
  545. return;
  546. selected_value_on_box_open = parent_element->GetAttribute<String>("value", "");
  547. selection_element->SetProperty(PropertyId::Visibility, Property(Style::Visibility::Visible));
  548. selection_element->SetPseudoClass("checked", true);
  549. value_element->SetPseudoClass("checked", true);
  550. button_element->SetPseudoClass("checked", true);
  551. box_layout_dirty = true;
  552. box_opened_since_last_format = true;
  553. AttachScrollEvent();
  554. box_visible = true;
  555. }
  556. void WidgetDropDown::HideSelectBox()
  557. {
  558. if (!box_visible)
  559. return;
  560. selection_element->SetProperty(PropertyId::Visibility, Property(Style::Visibility::Hidden));
  561. selection_element->RemoveProperty(PropertyId::Height);
  562. selection_element->SetPseudoClass("checked", false);
  563. value_element->SetPseudoClass("checked", false);
  564. button_element->SetPseudoClass("checked", false);
  565. DetachScrollEvent();
  566. box_visible = false;
  567. }
  568. void WidgetDropDown::CancelSelectBox()
  569. {
  570. if (!box_visible)
  571. return;
  572. if (parent_element->GetAttribute<String>("value", "") != selected_value_on_box_open)
  573. parent_element->SetAttribute("value", selected_value_on_box_open);
  574. HideSelectBox();
  575. }
  576. bool WidgetDropDown::IsSelectBoxVisible()
  577. {
  578. return box_visible;
  579. }
  580. } // namespace Rml