DropDownList.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../UI/Menu.h"
  24. namespace Urho3D
  25. {
  26. class ListView;
  27. /// %Menu %UI element that displays a popup list view.
  28. class URHO3D_API DropDownList : public Menu
  29. {
  30. URHO3D_OBJECT(DropDownList, Menu);
  31. public:
  32. /// Construct.
  33. explicit DropDownList(Context* context);
  34. /// Destruct.
  35. ~DropDownList() override;
  36. /// Register object factory.
  37. static void RegisterObject(Context* context);
  38. /// Apply attribute changes that can not be applied immediately.
  39. void ApplyAttributes() override;
  40. /// Return UI rendering batches.
  41. void GetBatches(PODVector<UIBatch>& batches, PODVector<float>& vertexData, const IntRect& currentScissor) override;
  42. /// React to the popup being shown.
  43. void OnShowPopup() override;
  44. /// React to the popup being hidden.
  45. void OnHidePopup() override;
  46. /// React to editable status change.
  47. void OnSetEditable() override;
  48. /// Add item to the end of the list.
  49. void AddItem(UIElement* item);
  50. /// Insert item to a specific position.
  51. void InsertItem(unsigned index, UIElement* item);
  52. /// Remove specific item.
  53. void RemoveItem(UIElement* item);
  54. /// Remove item at index.
  55. void RemoveItem(unsigned index);
  56. /// Remove all items.
  57. void RemoveAllItems();
  58. /// Set selection.
  59. /// @property
  60. void SetSelection(unsigned index);
  61. /// Set place holder text. This is the text shown when there is no selection (-1) in drop down list. Note that if the list has items, the default is to show the first item, so the "no selection" state has to be set explicitly.
  62. /// @property
  63. void SetPlaceholderText(const String& text);
  64. /// Set whether popup should be automatically resized to match the dropdown button width.
  65. /// @property
  66. void SetResizePopup(bool enable);
  67. /// Return number of items.
  68. /// @property
  69. unsigned GetNumItems() const;
  70. /// Return item at index.
  71. /// @property{get_items}
  72. UIElement* GetItem(unsigned index) const;
  73. /// Return all items.
  74. PODVector<UIElement*> GetItems() const;
  75. /// Return selection index, or M_MAX_UNSIGNED if none selected.
  76. /// @property
  77. unsigned GetSelection() const;
  78. /// Return selected item, or null if none selected.
  79. /// @property
  80. UIElement* GetSelectedItem() const;
  81. /// Return listview element.
  82. /// @property
  83. ListView* GetListView() const { return listView_; }
  84. /// Return selected item placeholder element.
  85. /// @property
  86. UIElement* GetPlaceholder() const { return placeholder_; }
  87. /// Return place holder text.
  88. /// @property
  89. const String& GetPlaceholderText() const;
  90. /// Return whether popup should be automatically resized.
  91. /// @property
  92. bool GetResizePopup() const { return resizePopup_; }
  93. /// Set selection attribute.
  94. void SetSelectionAttr(unsigned index);
  95. protected:
  96. /// Filter implicit attributes in serialization process.
  97. bool FilterImplicitAttributes(XMLElement& dest) const override;
  98. /// Filter implicit attributes in serialization process.
  99. bool FilterPopupImplicitAttributes(XMLElement& dest) const override;
  100. /// Listview element.
  101. SharedPtr<ListView> listView_;
  102. /// Selected item placeholder element.
  103. SharedPtr<UIElement> placeholder_;
  104. /// Resize popup flag.
  105. bool resizePopup_;
  106. private:
  107. /// Handle listview item click event.
  108. void HandleItemClicked(StringHash eventType, VariantMap& eventData);
  109. /// Handle a key press from the listview.
  110. void HandleListViewKey(StringHash eventType, VariantMap& eventData);
  111. /// Handle the listview selection change. Set placeholder text hidden/visible as necessary.
  112. void HandleSelectionChanged(StringHash eventType, VariantMap& eventData);
  113. /// Selected item index attribute.
  114. unsigned selectionAttr_;
  115. };
  116. }