WidgetDropDown.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. #ifndef RMLUI_CORE_ELEMENTS_WIDGETDROPDOWN_H
  29. #define RMLUI_CORE_ELEMENTS_WIDGETDROPDOWN_H
  30. #include "../../../Include/RmlUi/Core/EventListener.h"
  31. namespace Rml {
  32. class ElementFormControl;
  33. /**
  34. Widget for drop-down functionality.
  35. @author Lloyd Weehuizen
  36. */
  37. class WidgetDropDown : public EventListener {
  38. public:
  39. WidgetDropDown(ElementFormControl* element);
  40. virtual ~WidgetDropDown();
  41. /// Updates the select value rml if necessary.
  42. void OnUpdate();
  43. /// Updates the selection box layout if necessary.
  44. void OnRender();
  45. /// Positions the drop-down's internal elements.
  46. void OnLayout();
  47. /// Sets the value of the widget.
  48. void OnValueChange(const String& value);
  49. /// Sets the option element as the new selection.
  50. /// @param[in] option_element The option element to select.
  51. /// @param[in] force Forces the new selection, even if the widget believes the selection to not have changed.
  52. void SetSelection(Element* option_element, bool force = false);
  53. /// Seek to the next or previous valid (visible and not disabled) option.
  54. /// @param[in] seek_forward True to select the next valid option, false to select the previous valid option.
  55. void SeekSelection(bool seek_forward = true);
  56. /// Returns the index of the currently selected item.
  57. int GetSelection() const;
  58. /// Adds a new option to the select control.
  59. /// @param[in] rml The RML content used to represent the option.
  60. /// @param[in] value The value of the option.
  61. /// @param[in] before The index of the element to insert the new option before.
  62. /// @param[in] select True to select the new option.
  63. /// @param[in] selectable If true this option can be selected. If false, this option is not selectable.
  64. /// @return The index of the new option.
  65. int AddOption(const String& rml, const String& value, int before, bool select, bool selectable = true);
  66. /// Moves an option element to the select control.
  67. /// @param[in] element Element to move.
  68. /// @param[in] before The index of the element to insert the new option before.
  69. /// @return The index of the new option, or -1 if invalid.
  70. int AddOption(ElementPtr element, int before);
  71. /// Removes an option from the select control.
  72. /// @param[in] index The index of the option to remove.
  73. void RemoveOption(int index);
  74. /// Removes all options from the list.
  75. void ClearOptions();
  76. /// Returns one of the widget's options.
  77. /// @param[in] index The index of the desired option.
  78. /// @return The option element or nullptr if the index was out of bounds.
  79. Element* GetOption(int index);
  80. /// Returns the number of options in the widget.
  81. /// @return The number of options.
  82. int GetNumOptions() const;
  83. // Handle newly added option elements.
  84. void OnChildAdd(Element* element);
  85. // Handle newly removed option elements.
  86. void OnChildRemove(Element* element);
  87. /// Processes the incoming event.
  88. void ProcessEvent(Event& event) override;
  89. /// Shows the selection box.
  90. void ShowSelectBox();
  91. /// Hides the selection box.
  92. void HideSelectBox();
  93. /// Revert to the value selected when the selection box was opened, then hide the box.
  94. void CancelSelectBox();
  95. /// Check whether the select box is visible or not.
  96. bool IsSelectBoxVisible();
  97. private:
  98. void AttachScrollEvent();
  99. void DetachScrollEvent();
  100. // Parent element that holds this widget
  101. ElementFormControl* parent_element;
  102. // The elements making up the drop-down process.
  103. Element* button_element;
  104. Element* selection_element;
  105. Element* value_element;
  106. String selected_value_on_box_open;
  107. bool lock_selection = false;
  108. bool selection_dirty = false;
  109. bool value_rml_dirty = false;
  110. bool value_layout_dirty = false;
  111. bool value_changed_since_last_box_format = false;
  112. bool box_layout_dirty = false;
  113. bool box_opened_since_last_format = false;
  114. bool box_visible = false;
  115. };
  116. } // namespace Rml
  117. #endif