/* * 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 REGISTER_WIDGET(GUI, ComboBox) 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() { REGISTER_STRING_PROPERTY("placeholder", editor_placeholder, set_editor_placeholder); REGISTER_BOOL_PROPERTY("model_only", only_allow_values_from_model, set_only_allow_values_from_model); set_min_width(32); set_fixed_height(22); m_editor = add(); m_editor->set_frame_thickness(0); m_editor->on_return_pressed = [this] { if (on_return_pressed) on_return_pressed(); }; m_editor->on_up_pressed = [this] { navigate(AbstractView::CursorMovement::Up); }; m_editor->on_down_pressed = [this] { navigate(AbstractView::CursorMovement::Down); }; m_editor->on_pageup_pressed = [this] { navigate(AbstractView::CursorMovement::PageUp); }; m_editor->on_pagedown_pressed = [this] { navigate(AbstractView::CursorMovement::PageDown); }; m_editor->on_mousewheel = [this](int delta) { // Since we can only show one item at a time we don't want to // skip any. So just move one item at a time. navigate_relative(delta > 0 ? 1 : -1); }; m_editor->on_mousedown = [this] { if (only_allow_values_from_model()) m_open_button->click(); }; m_open_button = add