SpinBox.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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/Button.h>
  27. #include <LibGUI/SpinBox.h>
  28. #include <LibGUI/TextBox.h>
  29. REGISTER_WIDGET(GUI, SpinBox)
  30. namespace GUI {
  31. SpinBox::SpinBox()
  32. {
  33. set_min_width(32);
  34. set_fixed_height(22);
  35. m_editor = add<TextBox>();
  36. m_editor->set_text("0");
  37. m_editor->on_change = [this] {
  38. auto value = m_editor->text().to_uint();
  39. if (value.has_value())
  40. set_value(value.value());
  41. else
  42. m_editor->set_text(String::number(m_value));
  43. };
  44. m_editor->on_up_pressed = [this] {
  45. set_value(m_value + 1);
  46. };
  47. m_editor->on_down_pressed = [this] {
  48. set_value(m_value - 1);
  49. };
  50. m_increment_button = add<Button>();
  51. m_increment_button->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/upward-triangle.png"));
  52. m_increment_button->set_focus_policy(GUI::FocusPolicy::NoFocus);
  53. m_increment_button->on_click = [this](auto) { set_value(m_value + 1); };
  54. m_increment_button->set_auto_repeat_interval(150);
  55. m_decrement_button = add<Button>();
  56. m_decrement_button->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/downward-triangle.png"));
  57. m_decrement_button->set_focus_policy(GUI::FocusPolicy::NoFocus);
  58. m_decrement_button->on_click = [this](auto) { set_value(m_value - 1); };
  59. m_decrement_button->set_auto_repeat_interval(150);
  60. REGISTER_INT_PROPERTY("min", min, set_min);
  61. REGISTER_INT_PROPERTY("max", max, set_max);
  62. }
  63. SpinBox::~SpinBox()
  64. {
  65. }
  66. void SpinBox::set_value(int value)
  67. {
  68. value = clamp(value, m_min, m_max);
  69. if (m_value == value)
  70. return;
  71. m_value = value;
  72. m_editor->set_text(String::number(value));
  73. update();
  74. if (on_change)
  75. on_change(value);
  76. }
  77. void SpinBox::set_range(int min, int max)
  78. {
  79. VERIFY(min <= max);
  80. if (m_min == min && m_max == max)
  81. return;
  82. m_min = min;
  83. m_max = max;
  84. int old_value = m_value;
  85. m_value = clamp(m_value, m_min, m_max);
  86. if (m_value != old_value) {
  87. m_editor->set_text(String::number(m_value));
  88. if (on_change)
  89. on_change(m_value);
  90. }
  91. update();
  92. }
  93. void SpinBox::mousewheel_event(MouseEvent& event)
  94. {
  95. auto wheel_delta = event.wheel_delta() / abs(event.wheel_delta());
  96. if (event.modifiers() == KeyModifier::Mod_Ctrl)
  97. wheel_delta *= 6;
  98. set_value(m_value - wheel_delta);
  99. }
  100. void SpinBox::resize_event(ResizeEvent& event)
  101. {
  102. int frame_thickness = m_editor->frame_thickness();
  103. int button_height = (event.size().height() / 2) - frame_thickness;
  104. int button_width = 15;
  105. m_increment_button->set_relative_rect(width() - button_width - frame_thickness, frame_thickness, button_width, button_height);
  106. m_decrement_button->set_relative_rect(width() - button_width - frame_thickness, frame_thickness + button_height, button_width, button_height);
  107. m_editor->set_relative_rect(0, 0, width(), height());
  108. }
  109. }