ScrollableWidget.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 <LibGUI/ScrollBar.h>
  27. #include <LibGUI/ScrollableWidget.h>
  28. namespace GUI {
  29. ScrollableWidget::ScrollableWidget()
  30. {
  31. m_vertical_scrollbar = add<ScrollBar>(Orientation::Vertical);
  32. m_vertical_scrollbar->set_step(4);
  33. m_vertical_scrollbar->on_change = [this](int) {
  34. did_scroll();
  35. update();
  36. };
  37. m_horizontal_scrollbar = add<ScrollBar>(Orientation::Horizontal);
  38. m_horizontal_scrollbar->set_step(4);
  39. m_horizontal_scrollbar->set_big_step(30);
  40. m_horizontal_scrollbar->on_change = [this](int) {
  41. did_scroll();
  42. update();
  43. };
  44. m_corner_widget = add<Widget>();
  45. m_corner_widget->set_fill_with_background_color(true);
  46. }
  47. ScrollableWidget::~ScrollableWidget()
  48. {
  49. }
  50. void ScrollableWidget::mousewheel_event(MouseEvent& event)
  51. {
  52. if (!m_scrollbars_enabled) {
  53. event.ignore();
  54. return;
  55. }
  56. // FIXME: The wheel delta multiplier should probably come from... somewhere?
  57. if (event.shift()) {
  58. horizontal_scrollbar().set_value(horizontal_scrollbar().value() + event.wheel_delta() * 60);
  59. } else {
  60. vertical_scrollbar().set_value(vertical_scrollbar().value() + event.wheel_delta() * 20);
  61. }
  62. }
  63. void ScrollableWidget::custom_layout()
  64. {
  65. auto inner_rect = frame_inner_rect_for_size(size());
  66. int height_wanted_by_horizontal_scrollbar = m_horizontal_scrollbar->is_visible() ? m_horizontal_scrollbar->preferred_size().height() : 0;
  67. int width_wanted_by_vertical_scrollbar = m_vertical_scrollbar->is_visible() ? m_vertical_scrollbar->preferred_size().width() : 0;
  68. m_vertical_scrollbar->set_relative_rect(
  69. inner_rect.right() + 1 - m_vertical_scrollbar->preferred_size().width(),
  70. inner_rect.top(),
  71. m_vertical_scrollbar->preferred_size().width(),
  72. inner_rect.height() - height_wanted_by_horizontal_scrollbar);
  73. m_horizontal_scrollbar->set_relative_rect(
  74. inner_rect.left(),
  75. inner_rect.bottom() + 1 - m_horizontal_scrollbar->preferred_size().height(),
  76. inner_rect.width() - width_wanted_by_vertical_scrollbar,
  77. m_horizontal_scrollbar->preferred_size().height());
  78. m_corner_widget->set_visible(m_vertical_scrollbar->is_visible() && m_horizontal_scrollbar->is_visible());
  79. if (m_corner_widget->is_visible()) {
  80. Gfx::IntRect corner_rect { m_horizontal_scrollbar->relative_rect().right() + 1, m_vertical_scrollbar->relative_rect().bottom() + 1, width_occupied_by_vertical_scrollbar(), height_occupied_by_horizontal_scrollbar() };
  81. m_corner_widget->set_relative_rect(corner_rect);
  82. }
  83. }
  84. void ScrollableWidget::resize_event(ResizeEvent& event)
  85. {
  86. Frame::resize_event(event);
  87. update_scrollbar_ranges();
  88. }
  89. Gfx::IntSize ScrollableWidget::available_size() const
  90. {
  91. unsigned available_width = max(frame_inner_rect().width() - m_size_occupied_by_fixed_elements.width() - width_occupied_by_vertical_scrollbar(), 0);
  92. unsigned available_height = max(frame_inner_rect().height() - m_size_occupied_by_fixed_elements.height() - height_occupied_by_horizontal_scrollbar(), 0);
  93. return { available_width, available_height };
  94. }
  95. void ScrollableWidget::update_scrollbar_ranges()
  96. {
  97. auto available_size = this->available_size();
  98. int excess_height = max(0, m_content_size.height() - available_size.height());
  99. m_vertical_scrollbar->set_range(0, excess_height, available_size.height());
  100. if (should_hide_unnecessary_scrollbars())
  101. m_vertical_scrollbar->set_visible(excess_height > 0);
  102. int excess_width = max(0, m_content_size.width() - available_size.width());
  103. m_horizontal_scrollbar->set_range(0, excess_width, available_size.width());
  104. if (should_hide_unnecessary_scrollbars())
  105. m_horizontal_scrollbar->set_visible(excess_width > 0);
  106. m_vertical_scrollbar->set_big_step(visible_content_rect().height() - m_vertical_scrollbar->step());
  107. }
  108. void ScrollableWidget::set_content_size(const Gfx::IntSize& size)
  109. {
  110. if (m_content_size == size)
  111. return;
  112. m_content_size = size;
  113. update_scrollbar_ranges();
  114. }
  115. void ScrollableWidget::set_size_occupied_by_fixed_elements(const Gfx::IntSize& size)
  116. {
  117. if (m_size_occupied_by_fixed_elements == size)
  118. return;
  119. m_size_occupied_by_fixed_elements = size;
  120. update_scrollbar_ranges();
  121. }
  122. int ScrollableWidget::height_occupied_by_horizontal_scrollbar() const
  123. {
  124. return m_horizontal_scrollbar->is_visible() ? m_horizontal_scrollbar->height() : 0;
  125. }
  126. int ScrollableWidget::width_occupied_by_vertical_scrollbar() const
  127. {
  128. return m_vertical_scrollbar->is_visible() ? m_vertical_scrollbar->width() : 0;
  129. }
  130. Gfx::IntRect ScrollableWidget::visible_content_rect() const
  131. {
  132. Gfx::IntRect rect {
  133. m_horizontal_scrollbar->value(),
  134. m_vertical_scrollbar->value(),
  135. min(m_content_size.width(), frame_inner_rect().width() - width_occupied_by_vertical_scrollbar() - m_size_occupied_by_fixed_elements.width()),
  136. min(m_content_size.height(), frame_inner_rect().height() - height_occupied_by_horizontal_scrollbar() - m_size_occupied_by_fixed_elements.height())
  137. };
  138. if (rect.is_empty())
  139. return {};
  140. return rect;
  141. }
  142. void ScrollableWidget::scroll_into_view(const Gfx::IntRect& rect, Orientation orientation)
  143. {
  144. if (orientation == Orientation::Vertical)
  145. return scroll_into_view(rect, false, true);
  146. return scroll_into_view(rect, true, false);
  147. }
  148. void ScrollableWidget::scroll_into_view(const Gfx::IntRect& rect, bool scroll_horizontally, bool scroll_vertically)
  149. {
  150. auto visible_content_rect = this->visible_content_rect();
  151. if (visible_content_rect.contains(rect))
  152. return;
  153. if (scroll_vertically) {
  154. if (rect.top() < visible_content_rect.top()) {
  155. m_vertical_scrollbar->set_value(rect.top());
  156. } else if (rect.top() > visible_content_rect.top() && rect.bottom() > visible_content_rect.bottom()) {
  157. m_vertical_scrollbar->set_value(rect.bottom() - visible_content_rect.height() + 1);
  158. }
  159. }
  160. if (scroll_horizontally) {
  161. if (rect.left() < visible_content_rect.left()) {
  162. m_horizontal_scrollbar->set_value(rect.left());
  163. } else if (rect.left() > visible_content_rect.left() && rect.right() > visible_content_rect.right()) {
  164. m_horizontal_scrollbar->set_value(rect.right() - visible_content_rect.width() + 1);
  165. }
  166. }
  167. }
  168. void ScrollableWidget::set_scrollbars_enabled(bool scrollbars_enabled)
  169. {
  170. if (m_scrollbars_enabled == scrollbars_enabled)
  171. return;
  172. m_scrollbars_enabled = scrollbars_enabled;
  173. m_vertical_scrollbar->set_visible(m_scrollbars_enabled);
  174. m_horizontal_scrollbar->set_visible(m_scrollbars_enabled);
  175. m_corner_widget->set_visible(m_scrollbars_enabled);
  176. }
  177. void ScrollableWidget::scroll_to_top()
  178. {
  179. scroll_into_view({}, Orientation::Vertical);
  180. }
  181. void ScrollableWidget::scroll_to_bottom()
  182. {
  183. scroll_into_view({ 0, content_height(), 0, 0 }, Orientation::Vertical);
  184. }
  185. Gfx::IntRect ScrollableWidget::widget_inner_rect() const
  186. {
  187. auto rect = frame_inner_rect();
  188. rect.set_width(rect.width() - width_occupied_by_vertical_scrollbar());
  189. rect.set_height(rect.height() - height_occupied_by_horizontal_scrollbar());
  190. return rect;
  191. }
  192. Gfx::IntPoint ScrollableWidget::to_content_position(const Gfx::IntPoint& widget_position) const
  193. {
  194. auto content_position = widget_position;
  195. content_position.move_by(horizontal_scrollbar().value(), vertical_scrollbar().value());
  196. content_position.move_by(-frame_thickness(), -frame_thickness());
  197. return content_position;
  198. }
  199. Gfx::IntPoint ScrollableWidget::to_widget_position(const Gfx::IntPoint& content_position) const
  200. {
  201. auto widget_position = content_position;
  202. widget_position.move_by(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  203. widget_position.move_by(frame_thickness(), frame_thickness());
  204. return widget_position;
  205. }
  206. }