WidgetDropDown.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #ifndef ROCKETCONTROLSWIDGETDROPDOWN_H
  28. #define ROCKETCONTROLSWIDGETDROPDOWN_H
  29. #include <Rocket/Core/String.h>
  30. #include <Rocket/Core/EventListener.h>
  31. #include <Rocket/Controls/SelectOption.h>
  32. #include <vector>
  33. namespace Rocket {
  34. namespace Controls {
  35. class ElementFormControl;
  36. /**
  37. Widget for drop-down functionality.
  38. @author Lloyd Weehuizen
  39. */
  40. class WidgetDropDown : public Core::EventListener
  41. {
  42. public:
  43. WidgetDropDown(ElementFormControl* element);
  44. virtual ~WidgetDropDown();
  45. /// Updates the selection box layout if necessary.
  46. void OnRender();
  47. /// Positions the drop-down's internal elements.
  48. void OnLayout();
  49. /// Sets the value of the widget.
  50. /// @param[in] value The new value to set.
  51. void SetValue(const Rocket::Core::String& value);
  52. /// Returns the current value of the widget.
  53. /// @return The current value of the widget.
  54. const Rocket::Core::String& GetValue() const;
  55. /// Sets the index of the selection. If the new index lies outside of the bounds, the selection index will be set to -1.
  56. /// @param[in] selection The new selection index.
  57. /// @param[in] force Forces the new selection, even if the widget believes the selection to not have changed.
  58. void SetSelection(int selection, bool force = false);
  59. /// Returns the index of the currently selected item.
  60. /// @return The index of the currently selected item.
  61. int GetSelection() const;
  62. /// Adds a new option to the select control.
  63. /// @param[in] rml The RML content used to represent the option.
  64. /// @param[in] value The value of the option.
  65. /// @param[in] before The index of the element to insert the new option before.
  66. /// @param[in] select True to select the new option.
  67. /// @param[in] selectable If true this option can be selected. If false, this option is not selectable.
  68. /// @return The index of the new option.
  69. int AddOption(const Rocket::Core::String& rml, const Rocket::Core::String& value, int before, bool select, bool selectable = true);
  70. /// Removes an option from the select control.
  71. /// @param[in] index The index of the option to remove.
  72. void RemoveOption(int index);
  73. /// Removes all options from the list.
  74. void ClearOptions();
  75. /// Returns on of the widget's options.
  76. /// @param[in] The index of the desired option.
  77. /// @return The option. This may be NULL if the index was out of bounds.
  78. SelectOption* GetOption(int index);
  79. /// Returns the number of options in the widget.
  80. /// @return The number of options.
  81. int GetNumOptions() const;
  82. /// Processes the incoming event.
  83. virtual void ProcessEvent(Core::Event& event);
  84. private:
  85. typedef std::vector< SelectOption > OptionList;
  86. // Shows or hides the selection box.
  87. void ShowSelectBox(bool show);
  88. // Parent element that holds this widget
  89. ElementFormControl* parent_element;
  90. // The elements making up the drop-down process.
  91. Core::Element* button_element;
  92. Core::Element* selection_element;
  93. Core::Element* value_element;
  94. // The options in the drop down.
  95. OptionList options;
  96. int selected_option;
  97. // The current value of the widget.
  98. Rocket::Core::String value;
  99. bool box_layout_dirty;
  100. bool value_layout_dirty;
  101. };
  102. }
  103. }
  104. #endif