/* * Copyright (c) 2018-2020, Andreas Kling * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include #include #include #include #include #include #include #include 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 (!is_focused()) set_focus(true); if (on_mousewheel) on_mousewheel(event.wheel_delta()); } }; ComboBox::ComboBox() { m_editor = add(); m_editor->set_has_open_button(true); m_editor->on_change = [this] { if (on_change) on_change(m_editor->text(), m_list_view->selection().first()); }; m_editor->on_return_pressed = [this] { if (on_return_pressed) on_return_pressed(); }; m_editor->on_up_pressed = [this] { m_list_view->move_selection(-1); }; m_editor->on_down_pressed = [this] { m_list_view->move_selection(1); }; m_editor->on_pageup_pressed = [this] { m_list_view->move_selection(-m_list_view->selection().first().row()); }; m_editor->on_pagedown_pressed = [this] { if (model()) m_list_view->move_selection((model()->row_count() - 1) - m_list_view->selection().first().row()); }; m_editor->on_mousewheel = [this](int delta) { m_list_view->move_selection(delta); }; m_editor->on_mousedown = [this] { if (only_allow_values_from_model()) m_open_button->click(); }; m_open_button = add