Serenity Operating System
at portability 119 lines 3.8 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include <LibGUI/Button.h> 28#include <LibGUI/SpinBox.h> 29#include <LibGUI/TextBox.h> 30 31namespace GUI { 32 33SpinBox::SpinBox() 34{ 35 m_editor = add<TextBox>(); 36 m_editor->set_text("0"); 37 m_editor->on_change = [this] { 38 bool ok; 39 int value = m_editor->text().to_uint(ok); 40 if (ok) 41 set_value(value); 42 else 43 m_editor->set_text(String::number(m_value)); 44 }; 45 m_increment_button = add<Button>(); 46 m_increment_button->set_focusable(false); 47 m_increment_button->set_text("\xc3\xb6"); 48 m_increment_button->on_click = [this](auto&) { set_value(m_value + 1); }; 49 m_increment_button->set_auto_repeat_interval(150); 50 m_decrement_button = add<Button>(); 51 m_decrement_button->set_focusable(false); 52 m_decrement_button->set_text("\xc3\xb7"); 53 m_decrement_button->on_click = [this](auto&) { set_value(m_value - 1); }; 54 m_decrement_button->set_auto_repeat_interval(150); 55} 56 57SpinBox::~SpinBox() 58{ 59} 60 61void SpinBox::set_value(int value) 62{ 63 value = clamp(value, m_min, m_max); 64 if (m_value == value) 65 return; 66 m_value = value; 67 m_editor->set_text(String::number(value)); 68 update(); 69 if (on_change) 70 on_change(value); 71} 72 73void SpinBox::set_range(int min, int max) 74{ 75 ASSERT(min <= max); 76 if (m_min == min && m_max == max) 77 return; 78 79 m_min = min; 80 m_max = max; 81 82 int old_value = m_value; 83 m_value = clamp(m_value, m_min, m_max); 84 if (on_change && m_value != old_value) 85 on_change(m_value); 86 87 update(); 88} 89 90void SpinBox::keydown_event(KeyEvent& event) 91{ 92 if (event.key() == KeyCode::Key_Up) { 93 set_value(m_value + 1); 94 return; 95 } 96 if (event.key() == KeyCode::Key_Down) { 97 set_value(m_value - 1); 98 return; 99 } 100 101 event.ignore(); 102} 103 104void SpinBox::mousewheel_event(MouseEvent& event) 105{ 106 set_value(m_value - event.wheel_delta()); 107} 108 109void SpinBox::resize_event(ResizeEvent& event) 110{ 111 int frame_thickness = m_editor->frame_thickness(); 112 int button_height = (event.size().height() / 2) - frame_thickness; 113 int button_width = 15; 114 m_increment_button->set_relative_rect(width() - button_width - frame_thickness, frame_thickness, button_width, button_height); 115 m_decrement_button->set_relative_rect(width() - button_width - frame_thickness, frame_thickness + button_height, button_width, button_height); 116 m_editor->set_relative_rect(0, 0, width(), height()); 117} 118 119}