ScrollableWidget.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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_page_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->min_height() : 0;
  67. int width_wanted_by_vertical_scrollbar = m_vertical_scrollbar->is_visible() ? m_vertical_scrollbar->min_width() : 0;
  68. m_vertical_scrollbar->set_relative_rect(
  69. inner_rect.right() + 1 - m_vertical_scrollbar->min_width(),
  70. inner_rect.top(),
  71. m_vertical_scrollbar->min_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->min_height(),
  76. inner_rect.width() - width_wanted_by_vertical_scrollbar,
  77. m_horizontal_scrollbar->min_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);
  100. m_vertical_scrollbar->set_page_step(available_size.height());
  101. if (should_hide_unnecessary_scrollbars())
  102. m_vertical_scrollbar->set_visible(excess_height > 0);
  103. int excess_width = max(0, m_content_size.width() - available_size.width());
  104. m_horizontal_scrollbar->set_range(0, excess_width);
  105. m_horizontal_scrollbar->set_page_step(available_size.width());
  106. if (should_hide_unnecessary_scrollbars())
  107. m_horizontal_scrollbar->set_visible(excess_width > 0);
  108. m_vertical_scrollbar->set_page_step(visible_content_rect().height() - m_vertical_scrollbar->step());
  109. }
  110. void ScrollableWidget::set_content_size(const Gfx::IntSize& size)
  111. {
  112. if (m_content_size == size)
  113. return;
  114. m_content_size = size;
  115. update_scrollbar_ranges();
  116. }
  117. void ScrollableWidget::set_size_occupied_by_fixed_elements(const Gfx::IntSize& size)
  118. {
  119. if (m_size_occupied_by_fixed_elements == size)
  120. return;
  121. m_size_occupied_by_fixed_elements = size;
  122. update_scrollbar_ranges();
  123. }
  124. int ScrollableWidget::height_occupied_by_horizontal_scrollbar() const
  125. {
  126. return m_horizontal_scrollbar->is_visible() ? m_horizontal_scrollbar->height() : 0;
  127. }
  128. int ScrollableWidget::width_occupied_by_vertical_scrollbar() const
  129. {
  130. return m_vertical_scrollbar->is_visible() ? m_vertical_scrollbar->width() : 0;
  131. }
  132. Gfx::IntRect ScrollableWidget::visible_content_rect() const
  133. {
  134. Gfx::IntRect rect {
  135. m_horizontal_scrollbar->value(),
  136. m_vertical_scrollbar->value(),
  137. min(m_content_size.width(), frame_inner_rect().width() - width_occupied_by_vertical_scrollbar() - m_size_occupied_by_fixed_elements.width()),
  138. min(m_content_size.height(), frame_inner_rect().height() - height_occupied_by_horizontal_scrollbar() - m_size_occupied_by_fixed_elements.height())
  139. };
  140. if (rect.is_empty())
  141. return {};
  142. return rect;
  143. }
  144. void ScrollableWidget::scroll_into_view(const Gfx::IntRect& rect, Orientation orientation)
  145. {
  146. if (orientation == Orientation::Vertical)
  147. return scroll_into_view(rect, false, true);
  148. return scroll_into_view(rect, true, false);
  149. }
  150. void ScrollableWidget::scroll_into_view(const Gfx::IntRect& rect, bool scroll_horizontally, bool scroll_vertically)
  151. {
  152. auto visible_content_rect = this->visible_content_rect();
  153. if (visible_content_rect.contains(rect))
  154. return;
  155. if (scroll_vertically) {
  156. if (rect.top() < visible_content_rect.top()) {
  157. m_vertical_scrollbar->set_value(rect.top());
  158. } else if (rect.top() > visible_content_rect.top() && rect.bottom() > visible_content_rect.bottom()) {
  159. m_vertical_scrollbar->set_value(rect.bottom() - visible_content_rect.height() + 1);
  160. }
  161. }
  162. if (scroll_horizontally) {
  163. if (rect.left() < visible_content_rect.left()) {
  164. m_horizontal_scrollbar->set_value(rect.left());
  165. } else if (rect.left() > visible_content_rect.left() && rect.right() > visible_content_rect.right()) {
  166. m_horizontal_scrollbar->set_value(rect.right() - visible_content_rect.width() + 1);
  167. }
  168. }
  169. }
  170. void ScrollableWidget::set_scrollbars_enabled(bool scrollbars_enabled)
  171. {
  172. if (m_scrollbars_enabled == scrollbars_enabled)
  173. return;
  174. m_scrollbars_enabled = scrollbars_enabled;
  175. m_vertical_scrollbar->set_visible(m_scrollbars_enabled);
  176. m_horizontal_scrollbar->set_visible(m_scrollbars_enabled);
  177. m_corner_widget->set_visible(m_scrollbars_enabled);
  178. }
  179. void ScrollableWidget::scroll_to_top()
  180. {
  181. scroll_into_view({}, Orientation::Vertical);
  182. }
  183. void ScrollableWidget::scroll_to_bottom()
  184. {
  185. scroll_into_view({ 0, content_height(), 0, 0 }, Orientation::Vertical);
  186. }
  187. Gfx::IntRect ScrollableWidget::widget_inner_rect() const
  188. {
  189. auto rect = frame_inner_rect();
  190. rect.set_width(rect.width() - width_occupied_by_vertical_scrollbar());
  191. rect.set_height(rect.height() - height_occupied_by_horizontal_scrollbar());
  192. return rect;
  193. }
  194. Gfx::IntPoint ScrollableWidget::to_content_position(const Gfx::IntPoint& widget_position) const
  195. {
  196. auto content_position = widget_position;
  197. content_position.move_by(horizontal_scrollbar().value(), vertical_scrollbar().value());
  198. content_position.move_by(-frame_thickness(), -frame_thickness());
  199. return content_position;
  200. }
  201. Gfx::IntPoint ScrollableWidget::to_widget_position(const Gfx::IntPoint& content_position) const
  202. {
  203. auto widget_position = content_position;
  204. widget_position.move_by(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  205. widget_position.move_by(frame_thickness(), frame_thickness());
  206. return widget_position;
  207. }
  208. }