From e064999e0d9d6d91b846a9d8afe3e05e45559dd3 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Tue, 12 May 2020 15:10:31 +0200 Subject: LibGUI: Allow scrolling through a ComboBox with the mouse wheel --- Libraries/LibGUI/ComboBox.cpp | 25 +++++++++++++++++++++++- Libraries/LibGUI/ComboBox.h | 4 +++- Libraries/LibGUI/ListView.cpp | 45 ++++++++++++++++++++----------------------- Libraries/LibGUI/ListView.h | 3 +++ 4 files changed, 51 insertions(+), 26 deletions(-) diff --git a/Libraries/LibGUI/ComboBox.cpp b/Libraries/LibGUI/ComboBox.cpp index 62461a8399..160994c8fa 100644 --- a/Libraries/LibGUI/ComboBox.cpp +++ b/Libraries/LibGUI/ComboBox.cpp @@ -35,9 +35,28 @@ namespace GUI { +class ComboBoxEditor final : public TextEditor { + C_OBJECT(ComboBoxEditor); + +public: + Function on_mousewheel; + +private: + ComboBoxEditor() + : TextEditor(TextEditor::SingleLine) + { + } + + virtual void mousewheel_event(MouseEvent& event) override + { + if (on_mousewheel) + on_mousewheel(event.wheel_delta()); + } +}; + ComboBox::ComboBox() { - m_editor = add(); + m_editor = add(); m_editor->on_change = [this] { if (on_change) on_change(m_editor->text(), m_list_view->selection().first()); @@ -74,6 +93,10 @@ ComboBox::ComboBox() on_change(m_editor->text(), index); }); }; + + m_editor->on_mousewheel = [this](int delta) { + m_list_view->move_selection(delta); + }; } ComboBox::~ComboBox() diff --git a/Libraries/LibGUI/ComboBox.h b/Libraries/LibGUI/ComboBox.h index 8e3232996b..bb8d51877b 100644 --- a/Libraries/LibGUI/ComboBox.h +++ b/Libraries/LibGUI/ComboBox.h @@ -30,6 +30,8 @@ namespace GUI { +class ComboBoxEditor; + class ComboBox : public Widget { C_OBJECT(ComboBox) public: @@ -62,7 +64,7 @@ protected: virtual void resize_event(ResizeEvent&) override; private: - RefPtr m_editor; + RefPtr m_editor; RefPtr