Slider.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 <LibGfx/StylePainter.h>
  27. #include <LibGUI/Painter.h>
  28. #include <LibGUI/Slider.h>
  29. namespace GUI {
  30. Slider::Slider(Widget* parent)
  31. : Slider(Orientation::Horizontal, parent)
  32. {
  33. }
  34. Slider::Slider(Orientation orientation, Widget* parent)
  35. : Widget(parent)
  36. , m_orientation(orientation)
  37. {
  38. }
  39. Slider::~Slider()
  40. {
  41. }
  42. void Slider::set_range(int min, int max)
  43. {
  44. ASSERT(min <= max);
  45. if (m_min == min && m_max == max)
  46. return;
  47. m_min = min;
  48. m_max = max;
  49. m_value = clamp(m_value, m_min, m_max);
  50. update();
  51. }
  52. void Slider::set_value(int value)
  53. {
  54. value = clamp(value, m_min, m_max);
  55. if (m_value == value)
  56. return;
  57. m_value = value;
  58. update();
  59. if (on_value_changed)
  60. on_value_changed(m_value);
  61. }
  62. void Slider::paint_event(PaintEvent& event)
  63. {
  64. Painter painter(*this);
  65. painter.add_clip_rect(event.rect());
  66. Gfx::Rect track_rect;
  67. if (orientation() == Orientation::Horizontal) {
  68. track_rect = { inner_rect().x(), 0, inner_rect().width(), track_size() };
  69. track_rect.center_vertically_within(inner_rect());
  70. } else {
  71. track_rect = { 0, inner_rect().y(), track_size(), inner_rect().height() };
  72. track_rect.center_horizontally_within(inner_rect());
  73. }
  74. Gfx::StylePainter::paint_frame(painter, track_rect, palette(), Gfx::FrameShape::Panel, Gfx::FrameShadow::Sunken, 1);
  75. Gfx::StylePainter::paint_button(painter, knob_rect(), palette(), Gfx::ButtonStyle::Normal, false, m_knob_hovered);
  76. }
  77. Gfx::Rect Slider::knob_rect() const
  78. {
  79. auto inner_rect = this->inner_rect();
  80. Gfx::Rect rect;
  81. rect.set_secondary_offset_for_orientation(orientation(), 0);
  82. rect.set_secondary_size_for_orientation(orientation(), knob_secondary_size());
  83. if (knob_size_mode() == KnobSizeMode::Fixed) {
  84. if (m_max - m_min) {
  85. float scale = (float)inner_rect.primary_size_for_orientation(orientation()) / (float)(m_max - m_min);
  86. rect.set_primary_offset_for_orientation(orientation(), inner_rect.primary_offset_for_orientation(orientation()) + ((int)(m_value * scale)) - (knob_fixed_primary_size() / 2));
  87. } else
  88. rect.set_primary_size_for_orientation(orientation(), 0);
  89. rect.set_primary_size_for_orientation(orientation(), knob_fixed_primary_size());
  90. } else {
  91. float scale = (float)inner_rect.primary_size_for_orientation(orientation()) / (float)(m_max - m_min + 1);
  92. rect.set_primary_offset_for_orientation(orientation(), inner_rect.primary_offset_for_orientation(orientation()) + ((int)(m_value * scale)));
  93. if (m_max - m_min)
  94. rect.set_primary_size_for_orientation(orientation(), ::max((int)(scale), knob_fixed_primary_size()));
  95. else
  96. rect.set_primary_size_for_orientation(orientation(), inner_rect.primary_size_for_orientation(orientation()));
  97. }
  98. if (orientation() == Orientation::Horizontal)
  99. rect.center_vertically_within(inner_rect);
  100. else
  101. rect.center_horizontally_within(inner_rect);
  102. return rect;
  103. }
  104. void Slider::mousedown_event(MouseEvent& event)
  105. {
  106. if (!is_enabled())
  107. return;
  108. if (event.button() == MouseButton::Left) {
  109. if (knob_rect().contains(event.position())) {
  110. m_dragging = true;
  111. m_drag_origin = event.position();
  112. m_drag_origin_value = m_value;
  113. return;
  114. } else {
  115. if (event.position().primary_offset_for_orientation(orientation()) > knob_rect().last_edge_for_orientation(orientation()))
  116. set_value(m_value + 1);
  117. else if (event.position().primary_offset_for_orientation(orientation()) < knob_rect().first_edge_for_orientation(orientation()))
  118. set_value(m_value - 1);
  119. }
  120. }
  121. return Widget::mousedown_event(event);
  122. }
  123. void Slider::mousemove_event(MouseEvent& event)
  124. {
  125. if (!is_enabled())
  126. return;
  127. set_knob_hovered(knob_rect().contains(event.position()));
  128. if (m_dragging) {
  129. float delta = event.position().primary_offset_for_orientation(orientation()) - m_drag_origin.primary_offset_for_orientation(orientation());
  130. float scrubbable_range = inner_rect().primary_size_for_orientation(orientation());
  131. float value_steps_per_scrubbed_pixel = (m_max - m_min) / scrubbable_range;
  132. float new_value = m_drag_origin_value + (value_steps_per_scrubbed_pixel * delta);
  133. set_value((int)new_value);
  134. return;
  135. }
  136. return Widget::mousemove_event(event);
  137. }
  138. void Slider::mouseup_event(MouseEvent& event)
  139. {
  140. if (!is_enabled())
  141. return;
  142. if (event.button() == MouseButton::Left) {
  143. m_dragging = false;
  144. return;
  145. }
  146. return Widget::mouseup_event(event);
  147. }
  148. void Slider::leave_event(Core::Event& event)
  149. {
  150. if (!is_enabled())
  151. return;
  152. set_knob_hovered(false);
  153. Widget::leave_event(event);
  154. }
  155. void Slider::change_event(Event& event)
  156. {
  157. if (event.type() == Event::Type::EnabledChange) {
  158. if (!is_enabled())
  159. m_dragging = false;
  160. }
  161. Widget::change_event(event);
  162. }
  163. void Slider::set_knob_hovered(bool hovered)
  164. {
  165. if (m_knob_hovered == hovered)
  166. return;
  167. m_knob_hovered = hovered;
  168. update(knob_rect());
  169. }
  170. }