SpinBox.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #include <LibGUI/ControlBoxButton.h>
  27. #include <LibGUI/SpinBox.h>
  28. #include <LibGUI/TextBox.h>
  29. namespace GUI {
  30. SpinBox::SpinBox()
  31. {
  32. m_editor = add<TextBox>();
  33. m_editor->set_text("0");
  34. m_editor->on_change = [this] {
  35. auto value = m_editor->text().to_uint();
  36. if (value.has_value())
  37. set_value(value.value());
  38. else
  39. m_editor->set_text(String::number(m_value));
  40. };
  41. m_increment_button = add<ControlBoxButton>(ControlBoxButton::UpArrow);
  42. m_increment_button->set_focusable(false);
  43. m_increment_button->on_click = [this](auto) { set_value(m_value + 1); };
  44. m_increment_button->set_auto_repeat_interval(150);
  45. m_decrement_button = add<ControlBoxButton>(ControlBoxButton::DownArrow);
  46. m_decrement_button->set_focusable(false);
  47. m_decrement_button->on_click = [this](auto) { set_value(m_value - 1); };
  48. m_decrement_button->set_auto_repeat_interval(150);
  49. }
  50. SpinBox::~SpinBox()
  51. {
  52. }
  53. void SpinBox::set_value(int value)
  54. {
  55. value = clamp(value, m_min, m_max);
  56. if (m_value == value)
  57. return;
  58. m_value = value;
  59. m_editor->set_text(String::number(value));
  60. update();
  61. if (on_change)
  62. on_change(value);
  63. }
  64. void SpinBox::set_range(int min, int max)
  65. {
  66. ASSERT(min <= max);
  67. if (m_min == min && m_max == max)
  68. return;
  69. m_min = min;
  70. m_max = max;
  71. int old_value = m_value;
  72. m_value = clamp(m_value, m_min, m_max);
  73. if (m_value != old_value) {
  74. m_editor->set_text(String::number(m_value));
  75. if (on_change)
  76. on_change(m_value);
  77. }
  78. update();
  79. }
  80. void SpinBox::keydown_event(KeyEvent& event)
  81. {
  82. if (event.key() == KeyCode::Key_Up) {
  83. set_value(m_value + 1);
  84. return;
  85. }
  86. if (event.key() == KeyCode::Key_Down) {
  87. set_value(m_value - 1);
  88. return;
  89. }
  90. event.ignore();
  91. }
  92. void SpinBox::mousewheel_event(MouseEvent& event)
  93. {
  94. set_value(m_value - event.wheel_delta());
  95. }
  96. void SpinBox::resize_event(ResizeEvent& event)
  97. {
  98. int frame_thickness = m_editor->frame_thickness();
  99. int button_height = (event.size().height() / 2) - frame_thickness;
  100. int button_width = 15;
  101. m_increment_button->set_relative_rect(width() - button_width - frame_thickness, frame_thickness, button_width, button_height);
  102. m_decrement_button->set_relative_rect(width() - button_width - frame_thickness, frame_thickness + button_height, button_width, button_height);
  103. m_editor->set_relative_rect(0, 0, width(), height());
  104. }
  105. }