AbstractSlider.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. #include <LibGfx/StylePainter.h>
  12. namespace GUI {
  13. AbstractSlider::AbstractSlider(Orientation orientation)
  14. : m_orientation(orientation)
  15. {
  16. REGISTER_INT_PROPERTY("value", value, set_value);
  17. REGISTER_INT_PROPERTY("min", min, set_min);
  18. REGISTER_INT_PROPERTY("max", max, set_max);
  19. REGISTER_INT_PROPERTY("step", step, set_step);
  20. REGISTER_INT_PROPERTY("page_step", page_step, set_page_step);
  21. REGISTER_ENUM_PROPERTY("orientation", this->orientation, set_orientation, Orientation,
  22. { Orientation::Horizontal, "Horizontal" },
  23. { Orientation::Vertical, "Vertical" });
  24. }
  25. AbstractSlider::~AbstractSlider()
  26. {
  27. }
  28. void AbstractSlider::set_orientation(Orientation value)
  29. {
  30. if (m_orientation == value)
  31. return;
  32. m_orientation = value;
  33. update();
  34. }
  35. void AbstractSlider::set_page_step(int page_step)
  36. {
  37. m_page_step = AK::max(0, page_step);
  38. }
  39. void AbstractSlider::set_range(int min, int max)
  40. {
  41. VERIFY(min <= max);
  42. if (m_min == min && m_max == max)
  43. return;
  44. m_min = min;
  45. m_max = max;
  46. m_value = clamp(m_value, m_min, m_max);
  47. update();
  48. }
  49. void AbstractSlider::set_value(int value)
  50. {
  51. value = clamp(value, m_min, m_max);
  52. if (m_value == value)
  53. return;
  54. m_value = value;
  55. update();
  56. if (on_change)
  57. on_change(m_value);
  58. }
  59. }