WidgetDropDown.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 RMLUICONTROLSWIDGETDROPDOWN_H
  29. #define RMLUICONTROLSWIDGETDROPDOWN_H
  30. #include "../../Include/RmlUi/Core/String.h"
  31. #include "../../Include/RmlUi/Core/EventListener.h"
  32. #include "../../Include/RmlUi/Controls/SelectOption.h"
  33. #include <vector>
  34. namespace Rml {
  35. namespace Controls {
  36. class ElementFormControl;
  37. /**
  38. Widget for drop-down functionality.
  39. @author Lloyd Weehuizen
  40. */
  41. class WidgetDropDown : public Core::EventListener
  42. {
  43. public:
  44. WidgetDropDown(ElementFormControl* element);
  45. virtual ~WidgetDropDown();
  46. /// Updates the selection box layout if necessary.
  47. void OnRender();
  48. /// Positions the drop-down's internal elements.
  49. void OnLayout();
  50. /// Sets the value of the widget.
  51. /// @param[in] value The new value to set.
  52. void SetValue(const Rml::Core::String& value);
  53. /// Returns the current value of the widget.
  54. /// @return The current value of the widget.
  55. const Rml::Core::String& GetValue() const;
  56. /// Sets the index of the selection. If the new index lies outside of the bounds, the selection index will be set to -1.
  57. /// @param[in] selection The new selection index.
  58. /// @param[in] force Forces the new selection, even if the widget believes the selection to not have changed.
  59. void SetSelection(int selection, bool force = false);
  60. /// Returns the index of the currently selected item.
  61. /// @return The index of the currently selected item.
  62. int GetSelection() const;
  63. /// Adds a new option to the select control.
  64. /// @param[in] rml The RML content used to represent the option.
  65. /// @param[in] value The value of the option.
  66. /// @param[in] before The index of the element to insert the new option before.
  67. /// @param[in] select True to select the new option.
  68. /// @param[in] selectable If true this option can be selected. If false, this option is not selectable.
  69. /// @return The index of the new option.
  70. int AddOption(const Rml::Core::String& rml, const Rml::Core::String& value, int before, bool select, bool selectable = true);
  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 on of the widget's options.
  77. /// @param[in] The index of the desired option.
  78. /// @return The option. This may be NULL if the index was out of bounds.
  79. SelectOption* GetOption(int index);
  80. /// Returns the number of options in the widget.
  81. /// @return The number of options.
  82. int GetNumOptions() const;
  83. /// Processes the incoming event.
  84. virtual void ProcessEvent(Core::Event& event);
  85. private:
  86. typedef std::vector< SelectOption > OptionList;
  87. // Shows or hides the selection box.
  88. void ShowSelectBox(bool show);
  89. // Parent element that holds this widget
  90. ElementFormControl* parent_element;
  91. // The elements making up the drop-down process.
  92. Core::Element* button_element;
  93. Core::Element* selection_element;
  94. Core::Element* value_element;
  95. // The options in the drop down.
  96. OptionList options;
  97. int selected_option;
  98. // The current value of the widget.
  99. Rml::Core::String value;
  100. bool box_layout_dirty;
  101. bool value_layout_dirty;
  102. };
  103. }
  104. }
  105. #endif