DropDownList.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../UI/Menu.h"
  5. namespace Urho3D
  6. {
  7. class ListView;
  8. /// %Menu %UI element that displays a popup list view.
  9. class URHO3D_API DropDownList : public Menu
  10. {
  11. URHO3D_OBJECT(DropDownList, Menu);
  12. public:
  13. /// Construct.
  14. explicit DropDownList(Context* context);
  15. /// Destruct.
  16. ~DropDownList() override;
  17. /// Register object factory.
  18. /// @nobind
  19. static void RegisterObject(Context* context);
  20. /// Apply attribute changes that can not be applied immediately.
  21. void ApplyAttributes() override;
  22. /// Return UI rendering batches.
  23. void GetBatches(Vector<UIBatch>& batches, Vector<float>& vertexData, const IntRect& currentScissor) override;
  24. /// React to the popup being shown.
  25. void OnShowPopup() override;
  26. /// React to the popup being hidden.
  27. void OnHidePopup() override;
  28. /// React to editable status change.
  29. void OnSetEditable() override;
  30. /// Add item to the end of the list.
  31. void AddItem(UIElement* item);
  32. /// Insert item to a specific position. index can be ENDPOS.
  33. void InsertItem(i32 index, UIElement* item);
  34. /// Remove specific item.
  35. void RemoveItem(UIElement* item);
  36. /// Remove item at index.
  37. void RemoveItem(i32 index);
  38. /// Remove all items.
  39. void RemoveAllItems();
  40. /// Set selection.
  41. /// @property
  42. void SetSelection(i32 index);
  43. /// 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.
  44. /// @property
  45. void SetPlaceholderText(const String& text);
  46. /// Set whether popup should be automatically resized to match the dropdown button width.
  47. /// @property
  48. void SetResizePopup(bool enable);
  49. /// Return number of items.
  50. /// @property
  51. i32 GetNumItems() const;
  52. /// Return item at index.
  53. /// @property{get_items}
  54. UIElement* GetItem(i32 index) const;
  55. /// Return all items.
  56. Vector<UIElement*> GetItems() const;
  57. /// Return selection index, or NINDEX if none selected.
  58. /// @property
  59. i32 GetSelection() const;
  60. /// Return selected item, or null if none selected.
  61. /// @property
  62. UIElement* GetSelectedItem() const;
  63. /// Return listview element.
  64. /// @property
  65. ListView* GetListView() const { return listView_; }
  66. /// Return selected item placeholder element.
  67. /// @property
  68. UIElement* GetPlaceholder() const { return placeholder_; }
  69. /// Return place holder text.
  70. /// @property
  71. const String& GetPlaceholderText() const;
  72. /// Return whether popup should be automatically resized.
  73. /// @property
  74. bool GetResizePopup() const { return resizePopup_; }
  75. /// Set selection attribute.
  76. void SetSelectionAttr(i32 index);
  77. protected:
  78. /// Filter implicit attributes in serialization process.
  79. bool FilterImplicitAttributes(XMLElement& dest) const override;
  80. /// Filter implicit attributes in serialization process.
  81. bool FilterPopupImplicitAttributes(XMLElement& dest) const override;
  82. /// Listview element.
  83. SharedPtr<ListView> listView_;
  84. /// Selected item placeholder element.
  85. SharedPtr<UIElement> placeholder_;
  86. /// Resize popup flag.
  87. bool resizePopup_;
  88. private:
  89. /// Handle listview item click event.
  90. void HandleItemClicked(StringHash eventType, VariantMap& eventData);
  91. /// Handle a key press from the listview.
  92. void HandleListViewKey(StringHash eventType, VariantMap& eventData);
  93. /// Handle the listview selection change. Set placeholder text hidden/visible as necessary.
  94. void HandleSelectionChanged(StringHash eventType, VariantMap& eventData);
  95. /// Selected item index attribute.
  96. i32 selectionAttr_;
  97. };
  98. }