WidgetDropDown.h 4.6 KB

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