| 1 | // Aseprite UI Library |
| 2 | // Copyright (C) 2019-2021 Igara Studio S.A. |
| 3 | // Copyright (C) 2001-2017 David Capello |
| 4 | // |
| 5 | // This file is released under the terms of the MIT license. |
| 6 | // Read LICENSE.txt for more information. |
| 7 | |
| 8 | #ifndef UI_COMBOBOX_H_INCLUDED |
| 9 | #define UI_COMBOBOX_H_INCLUDED |
| 10 | #pragma once |
| 11 | |
| 12 | #include "obs/signal.h" |
| 13 | #include "ui/widget.h" |
| 14 | |
| 15 | #include <string> |
| 16 | #include <vector> |
| 17 | |
| 18 | namespace ui { |
| 19 | |
| 20 | class Button; |
| 21 | class Entry; |
| 22 | class Event; |
| 23 | class ListBox; |
| 24 | class Window; |
| 25 | |
| 26 | class ComboBoxEntry; |
| 27 | class ComboBoxListBox; |
| 28 | |
| 29 | class ComboBox : public Widget { |
| 30 | friend class ComboBoxEntry; |
| 31 | friend class ComboBoxListBox; |
| 32 | |
| 33 | public: |
| 34 | typedef std::vector<Widget*> Items; |
| 35 | |
| 36 | ComboBox(); |
| 37 | ~ComboBox(); |
| 38 | |
| 39 | Items::iterator begin() { return m_items.begin(); } |
| 40 | Items::iterator end() { return m_items.end(); } |
| 41 | |
| 42 | void setEditable(bool state); |
| 43 | void setClickOpen(bool state); |
| 44 | void setCaseSensitive(bool state); |
| 45 | void setUseCustomWidget(bool state); |
| 46 | |
| 47 | bool isEditable() const { return m_editable; } |
| 48 | bool isClickOpen() const { return m_clickopen; } |
| 49 | bool isCaseSensitive() const { return m_casesensitive; } |
| 50 | bool useCustomWidget() const { return m_useCustomWidget; } |
| 51 | |
| 52 | int addItem(Widget* item); |
| 53 | int addItem(const std::string& text); |
| 54 | void insertItem(int itemIndex, Widget* item); |
| 55 | void insertItem(int itemIndex, const std::string& text); |
| 56 | |
| 57 | // Removes the given item (you must delete it). |
| 58 | void removeItem(Widget* item); |
| 59 | |
| 60 | // Removes and deletes the given item. |
| 61 | void deleteItem(int itemIndex); |
| 62 | |
| 63 | void deleteAllItems(); |
| 64 | |
| 65 | int getItemCount() const; |
| 66 | |
| 67 | Widget* getItem(const int itemIndex) const; |
| 68 | const std::string& getItemText(int itemIndex) const; |
| 69 | void setItemText(int itemIndex, const std::string& text); |
| 70 | int findItemIndex(const std::string& text) const; |
| 71 | int findItemIndexByValue(const std::string& value) const; |
| 72 | |
| 73 | Widget* getSelectedItem() const; |
| 74 | void setSelectedItem(Widget* item); |
| 75 | |
| 76 | int getSelectedItemIndex() const; |
| 77 | void setSelectedItemIndex(int itemIndex); |
| 78 | |
| 79 | std::string getValue() const; |
| 80 | void setValue(const std::string& value); |
| 81 | |
| 82 | Entry* getEntryWidget(); |
| 83 | Button* getButtonWidget(); |
| 84 | Window* getWindowWidget() { return m_window; } |
| 85 | |
| 86 | void openListBox(); |
| 87 | void closeListBox(); |
| 88 | void switchListBox(); |
| 89 | |
| 90 | // Signals |
| 91 | obs::signal<void()> Change; |
| 92 | obs::signal<void()> OpenListBox; |
| 93 | obs::signal<void()> CloseListBox; |
| 94 | |
| 95 | protected: |
| 96 | bool onProcessMessage(Message* msg) override; |
| 97 | void onResize(ResizeEvent& ev) override; |
| 98 | void onSizeHint(SizeHintEvent& ev) override; |
| 99 | void onInitTheme(InitThemeEvent& ev) override; |
| 100 | |
| 101 | // When the selected item is changed. |
| 102 | virtual void onChange(); |
| 103 | |
| 104 | // When the text of an editable ComboBox is changed. |
| 105 | virtual void onEntryChange(); |
| 106 | |
| 107 | // Before we open the list box, we can fill the combobox with the |
| 108 | // items to show. TODO replace all this with a MVC-like combobox |
| 109 | // model so we request items only when it's required. |
| 110 | virtual void onBeforeOpenListBox(); |
| 111 | |
| 112 | virtual void onOpenListBox(); |
| 113 | virtual void onCloseListBox(); |
| 114 | virtual void onEnterOnEditableEntry(); |
| 115 | |
| 116 | private: |
| 117 | void updateListBoxPos(); |
| 118 | void onButtonClick(Event& ev); |
| 119 | void filterMessages(); |
| 120 | void removeMessageFilters(); |
| 121 | void putSelectedItemAsCustomWidget(); |
| 122 | |
| 123 | ComboBoxEntry* m_entry; |
| 124 | Button* m_button; |
| 125 | Window* m_window; |
| 126 | ComboBoxListBox* m_listbox; |
| 127 | Items m_items; |
| 128 | int m_selected; |
| 129 | bool m_editable; |
| 130 | bool m_clickopen; |
| 131 | bool m_casesensitive; |
| 132 | bool m_filtering; |
| 133 | bool m_useCustomWidget; |
| 134 | }; |
| 135 | |
| 136 | } // namespace ui |
| 137 | |
| 138 | #endif |
| 139 | |