ComboBox.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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&, AllowCallback = AllowCallback::Yes);
  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. void clear_selection();
  27. bool only_allow_values_from_model() const { return m_only_allow_values_from_model; }
  28. void set_only_allow_values_from_model(bool);
  29. int model_column() const;
  30. void set_model_column(int);
  31. void set_editor_placeholder(StringView placeholder);
  32. String const& editor_placeholder() const;
  33. int max_visible_items() const { return m_max_visible_items; }
  34. void set_max_visible_items(int max) { m_max_visible_items = max; }
  35. Function<void(String const&, ModelIndex const&)> on_change;
  36. Function<void()> on_return_pressed;
  37. protected:
  38. ComboBox();
  39. virtual void resize_event(ResizeEvent&) override;
  40. private:
  41. void selection_updated(ModelIndex const&);
  42. void navigate(AbstractView::CursorMovement);
  43. void navigate_relative(int);
  44. RefPtr<ComboBoxEditor> m_editor;
  45. RefPtr<Button> m_open_button;
  46. RefPtr<Window> m_list_window;
  47. RefPtr<ListView> m_list_view;
  48. Optional<ModelIndex> m_selected_index;
  49. bool m_only_allow_values_from_model { false };
  50. bool m_updating_model { false };
  51. int m_max_visible_items { 15 };
  52. };
  53. }