GSlider.cpp 6.5 KB

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