SpinBox.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGUI/Button.h>
  7. #include <LibGUI/SpinBox.h>
  8. #include <LibGUI/TextBox.h>
  9. REGISTER_WIDGET(GUI, SpinBox)
  10. namespace GUI {
  11. SpinBox::SpinBox()
  12. {
  13. set_min_width(32);
  14. set_fixed_height(22);
  15. m_editor = add<TextBox>();
  16. m_editor->set_text("0");
  17. m_editor->on_change = [this, weak_this = make_weak_ptr()] {
  18. if (!weak_this)
  19. return;
  20. auto value = m_editor->text().to_uint();
  21. if (value.has_value())
  22. set_value(value.value());
  23. else
  24. m_editor->set_text(String::number(m_value));
  25. };
  26. m_editor->on_up_pressed = [this] {
  27. set_value(m_value + 1);
  28. };
  29. m_editor->on_down_pressed = [this] {
  30. set_value(m_value - 1);
  31. };
  32. m_increment_button = add<Button>();
  33. m_increment_button->set_button_style(Gfx::ButtonStyle::ThickCap);
  34. m_increment_button->set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/upward-triangle.png").release_value_but_fixme_should_propagate_errors());
  35. m_increment_button->set_focus_policy(GUI::FocusPolicy::NoFocus);
  36. m_increment_button->on_click = [this](auto) { set_value(m_value + 1); };
  37. m_increment_button->set_auto_repeat_interval(150);
  38. m_decrement_button = add<Button>();
  39. m_decrement_button->set_button_style(Gfx::ButtonStyle::ThickCap);
  40. m_decrement_button->set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/downward-triangle.png").release_value_but_fixme_should_propagate_errors());
  41. m_decrement_button->set_focus_policy(GUI::FocusPolicy::NoFocus);
  42. m_decrement_button->on_click = [this](auto) { set_value(m_value - 1); };
  43. m_decrement_button->set_auto_repeat_interval(150);
  44. REGISTER_INT_PROPERTY("min", min, set_min);
  45. REGISTER_INT_PROPERTY("max", max, set_max);
  46. }
  47. SpinBox::~SpinBox()
  48. {
  49. }
  50. void SpinBox::set_value(int value, AllowCallback allow_callback)
  51. {
  52. value = clamp(value, m_min, m_max);
  53. if (m_value == value)
  54. return;
  55. m_value = value;
  56. m_editor->set_text(String::number(value));
  57. update();
  58. if (on_change && allow_callback == AllowCallback::Yes)
  59. on_change(value);
  60. }
  61. void SpinBox::set_range(int min, int max, AllowCallback allow_callback)
  62. {
  63. VERIFY(min <= max);
  64. if (m_min == min && m_max == max)
  65. return;
  66. m_min = min;
  67. m_max = max;
  68. int old_value = m_value;
  69. m_value = clamp(m_value, m_min, m_max);
  70. if (m_value != old_value) {
  71. m_editor->set_text(String::number(m_value));
  72. if (on_change && allow_callback == AllowCallback::Yes)
  73. on_change(m_value);
  74. }
  75. update();
  76. }
  77. void SpinBox::mousewheel_event(MouseEvent& event)
  78. {
  79. auto wheel_delta = event.wheel_delta_y() / abs(event.wheel_delta_y());
  80. if (event.modifiers() == KeyModifier::Mod_Ctrl)
  81. wheel_delta *= 6;
  82. set_value(m_value - wheel_delta);
  83. }
  84. void SpinBox::resize_event(ResizeEvent& event)
  85. {
  86. int frame_thickness = m_editor->frame_thickness();
  87. int button_height = (event.size().height() / 2) - frame_thickness;
  88. int button_width = 15;
  89. m_increment_button->set_relative_rect(width() - button_width - frame_thickness, frame_thickness, button_width, button_height);
  90. m_decrement_button->set_relative_rect(width() - button_width - frame_thickness, frame_thickness + button_height, button_width, button_height);
  91. m_editor->set_relative_rect(0, 0, width(), height());
  92. }
  93. }