AbstractSlider.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Assertions.h>
  7. #include <AK/StdLibExtras.h>
  8. #include <LibGUI/Painter.h>
  9. #include <LibGUI/Slider.h>
  10. #include <LibGfx/Palette.h>
  11. namespace GUI {
  12. AbstractSlider::AbstractSlider(Orientation orientation)
  13. : m_orientation(orientation)
  14. {
  15. REGISTER_INT_PROPERTY("value", value, set_value);
  16. REGISTER_INT_PROPERTY("min", min, set_min);
  17. REGISTER_INT_PROPERTY("max", max, set_max);
  18. REGISTER_INT_PROPERTY("step", step, set_step);
  19. REGISTER_INT_PROPERTY("page_step", page_step, set_page_step);
  20. REGISTER_ENUM_PROPERTY("orientation", this->orientation, set_orientation, Orientation,
  21. { Orientation::Horizontal, "Horizontal" },
  22. { Orientation::Vertical, "Vertical" });
  23. }
  24. AbstractSlider::~AbstractSlider()
  25. {
  26. }
  27. void AbstractSlider::set_orientation(Orientation value)
  28. {
  29. if (m_orientation == value)
  30. return;
  31. m_orientation = value;
  32. update();
  33. }
  34. void AbstractSlider::set_page_step(int page_step)
  35. {
  36. m_page_step = AK::max(0, page_step);
  37. }
  38. void AbstractSlider::set_range(int min, int max)
  39. {
  40. VERIFY(min <= max);
  41. if (m_min == min && m_max == max)
  42. return;
  43. m_min = min;
  44. m_max = max;
  45. m_value = clamp(m_value, m_min, m_max);
  46. update();
  47. }
  48. void AbstractSlider::set_value(int value, AllowCallback allow_callback)
  49. {
  50. value = clamp(value, m_min, m_max);
  51. if (m_value == value)
  52. return;
  53. m_value = value;
  54. update();
  55. if (on_change && allow_callback == AllowCallback::Yes)
  56. on_change(m_value);
  57. }
  58. }