ComboBox.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibGUI/AbstractView.h>
  8. #include <LibGUI/Frame.h>
  9. #include <LibGUI/Model.h>
  10. namespace GUI {
  11. class ComboBoxEditor;
  12. class ComboBox : public Frame {
  13. C_OBJECT(ComboBox);
  14. public:
  15. virtual ~ComboBox() override;
  16. String text() const;
  17. void set_text(String const&);
  18. void open();
  19. void close();
  20. void select_all();
  21. Model* model();
  22. Model const* model() const;
  23. void set_model(NonnullRefPtr<Model>);
  24. size_t selected_index() const;
  25. void set_selected_index(size_t index, AllowCallback = AllowCallback::Yes);
  26. bool only_allow_values_from_model() const { return m_only_allow_values_from_model; }
  27. void set_only_allow_values_from_model(bool);
  28. int model_column() const;
  29. void set_model_column(int);
  30. void set_editor_placeholder(StringView placeholder);
  31. String const& editor_placeholder() const;
  32. Function<void(String const&, ModelIndex const&)> on_change;
  33. Function<void()> on_return_pressed;
  34. protected:
  35. ComboBox();
  36. virtual void resize_event(ResizeEvent&) override;
  37. private:
  38. void selection_updated(ModelIndex const&);
  39. void navigate(AbstractView::CursorMovement);
  40. void navigate_relative(int);
  41. RefPtr<ComboBoxEditor> m_editor;
  42. RefPtr<Button> m_open_button;
  43. RefPtr<Window> m_list_window;
  44. RefPtr<ListView> m_list_view;
  45. Optional<ModelIndex> m_selected_index;
  46. bool m_only_allow_values_from_model { false };
  47. bool m_updating_model { false };
  48. };
  49. }