ScrollBar.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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 <LibCore/Timer.h>
  27. #include <LibGUI/Painter.h>
  28. #include <LibGUI/ScrollBar.h>
  29. #include <LibGfx/CharacterBitmap.h>
  30. #include <LibGfx/Palette.h>
  31. #include <LibGfx/StylePainter.h>
  32. REGISTER_WIDGET(GUI, ScrollBar)
  33. namespace GUI {
  34. static const char* s_up_arrow_bitmap_data = {
  35. " "
  36. " # "
  37. " ### "
  38. " ##### "
  39. " ####### "
  40. " ### "
  41. " ### "
  42. " ### "
  43. " "
  44. };
  45. static const char* s_down_arrow_bitmap_data = {
  46. " "
  47. " ### "
  48. " ### "
  49. " ### "
  50. " ####### "
  51. " ##### "
  52. " ### "
  53. " # "
  54. " "
  55. };
  56. static const char* s_left_arrow_bitmap_data = {
  57. " "
  58. " # "
  59. " ## "
  60. " ###### "
  61. " ####### "
  62. " ###### "
  63. " ## "
  64. " # "
  65. " "
  66. };
  67. static const char* s_right_arrow_bitmap_data = {
  68. " "
  69. " # "
  70. " ## "
  71. " ###### "
  72. " ####### "
  73. " ###### "
  74. " ## "
  75. " # "
  76. " "
  77. };
  78. static Gfx::CharacterBitmap* s_up_arrow_bitmap;
  79. static Gfx::CharacterBitmap* s_down_arrow_bitmap;
  80. static Gfx::CharacterBitmap* s_left_arrow_bitmap;
  81. static Gfx::CharacterBitmap* s_right_arrow_bitmap;
  82. ScrollBar::ScrollBar(Orientation orientation)
  83. : AbstractSlider(orientation)
  84. {
  85. m_automatic_scrolling_timer = add<Core::Timer>();
  86. if (!s_up_arrow_bitmap)
  87. s_up_arrow_bitmap = &Gfx::CharacterBitmap::create_from_ascii(s_up_arrow_bitmap_data, 9, 9).leak_ref();
  88. if (!s_down_arrow_bitmap)
  89. s_down_arrow_bitmap = &Gfx::CharacterBitmap::create_from_ascii(s_down_arrow_bitmap_data, 9, 9).leak_ref();
  90. if (!s_left_arrow_bitmap)
  91. s_left_arrow_bitmap = &Gfx::CharacterBitmap::create_from_ascii(s_left_arrow_bitmap_data, 9, 9).leak_ref();
  92. if (!s_right_arrow_bitmap)
  93. s_right_arrow_bitmap = &Gfx::CharacterBitmap::create_from_ascii(s_right_arrow_bitmap_data, 9, 9).leak_ref();
  94. if (orientation == Orientation::Vertical) {
  95. set_fixed_width(16);
  96. } else {
  97. set_fixed_height(16);
  98. }
  99. m_automatic_scrolling_timer->set_interval(100);
  100. m_automatic_scrolling_timer->on_timeout = [this] {
  101. on_automatic_scrolling_timer_fired();
  102. };
  103. }
  104. ScrollBar::~ScrollBar()
  105. {
  106. }
  107. Gfx::IntRect ScrollBar::decrement_button_rect() const
  108. {
  109. return { 0, 0, button_width(), button_height() };
  110. }
  111. Gfx::IntRect ScrollBar::increment_button_rect() const
  112. {
  113. if (orientation() == Orientation::Vertical)
  114. return { 0, height() - button_height(), button_width(), button_height() };
  115. else
  116. return { width() - button_width(), 0, button_width(), button_height() };
  117. }
  118. Gfx::IntRect ScrollBar::decrement_gutter_rect() const
  119. {
  120. if (orientation() == Orientation::Vertical)
  121. return { 0, button_height(), button_width(), scrubber_rect().top() - button_height() };
  122. else
  123. return { button_width(), 0, scrubber_rect().x() - button_width(), button_height() };
  124. }
  125. Gfx::IntRect ScrollBar::increment_gutter_rect() const
  126. {
  127. auto scrubber_rect = this->scrubber_rect();
  128. if (orientation() == Orientation::Vertical)
  129. return { 0, scrubber_rect.bottom() + 1, button_width(), height() - button_height() - scrubber_rect.bottom() - 1 };
  130. else
  131. return { scrubber_rect.right() + 1, 0, width() - button_width() - scrubber_rect.right() - 1, button_width() };
  132. }
  133. int ScrollBar::scrubbable_range_in_pixels() const
  134. {
  135. if (orientation() == Orientation::Vertical)
  136. return height() - button_height() * 2 - visible_scrubber_size();
  137. else
  138. return width() - button_width() * 2 - visible_scrubber_size();
  139. }
  140. bool ScrollBar::has_scrubber() const
  141. {
  142. return max() != min();
  143. }
  144. int ScrollBar::unclamped_scrubber_size() const
  145. {
  146. int pixel_range = length(orientation()) - button_size() * 2;
  147. int value_range = max() - min();
  148. int scrubber_size = 0;
  149. if (value_range > 0) {
  150. // Scrubber size should be proportional to the visible portion
  151. // (page) in relation to the content (value range + page)
  152. scrubber_size = (page_step() * pixel_range) / (value_range + page_step());
  153. }
  154. return scrubber_size;
  155. }
  156. int ScrollBar::visible_scrubber_size() const
  157. {
  158. return ::max(unclamped_scrubber_size(), button_size());
  159. }
  160. Gfx::IntRect ScrollBar::scrubber_rect() const
  161. {
  162. if (!has_scrubber() || length(orientation()) <= (button_size() * 2) + visible_scrubber_size())
  163. return {};
  164. float x_or_y;
  165. if (value() == min())
  166. x_or_y = button_size();
  167. else if (value() == max())
  168. x_or_y = (length(orientation()) - button_size() - visible_scrubber_size()) + 1;
  169. else {
  170. float range_size = max() - min();
  171. float available = scrubbable_range_in_pixels();
  172. float step = available / range_size;
  173. x_or_y = (button_size() + (step * value()));
  174. }
  175. if (orientation() == Orientation::Vertical)
  176. return { 0, (int)x_or_y, button_width(), visible_scrubber_size() };
  177. else
  178. return { (int)x_or_y, 0, visible_scrubber_size(), button_height() };
  179. }
  180. void ScrollBar::paint_event(PaintEvent& event)
  181. {
  182. Painter painter(*this);
  183. painter.add_clip_rect(event.rect());
  184. Component hovered_component_for_painting = m_hovered_component;
  185. if (!has_scrubber() || (m_pressed_component != Component::None && m_hovered_component != m_pressed_component))
  186. hovered_component_for_painting = Component::None;
  187. painter.fill_rect_with_dither_pattern(rect(), palette().button().lightened(1.3f), palette().button());
  188. bool decrement_pressed = m_pressed_component == Component::DecrementButton;
  189. bool increment_pressed = m_pressed_component == Component::IncrementButton;
  190. Gfx::StylePainter::paint_button(painter, decrement_button_rect(), palette(), Gfx::ButtonStyle::Normal, decrement_pressed, hovered_component_for_painting == Component::DecrementButton);
  191. Gfx::StylePainter::paint_button(painter, increment_button_rect(), palette(), Gfx::ButtonStyle::Normal, increment_pressed, hovered_component_for_painting == Component::IncrementButton);
  192. if (length(orientation()) > default_button_size()) {
  193. auto decrement_location = decrement_button_rect().location().translated(3, 3);
  194. if (decrement_pressed)
  195. decrement_location.move_by(1, 1);
  196. if (!has_scrubber() || !is_enabled())
  197. painter.draw_bitmap(decrement_location.translated(1, 1), orientation() == Orientation::Vertical ? *s_up_arrow_bitmap : *s_left_arrow_bitmap, palette().threed_highlight());
  198. painter.draw_bitmap(decrement_location, orientation() == Orientation::Vertical ? *s_up_arrow_bitmap : *s_left_arrow_bitmap, (has_scrubber() && is_enabled()) ? palette().button_text() : palette().threed_shadow1());
  199. auto increment_location = increment_button_rect().location().translated(3, 3);
  200. if (increment_pressed)
  201. increment_location.move_by(1, 1);
  202. if (!has_scrubber() || !is_enabled())
  203. painter.draw_bitmap(increment_location.translated(1, 1), orientation() == Orientation::Vertical ? *s_down_arrow_bitmap : *s_right_arrow_bitmap, palette().threed_highlight());
  204. painter.draw_bitmap(increment_location, orientation() == Orientation::Vertical ? *s_down_arrow_bitmap : *s_right_arrow_bitmap, (has_scrubber() && is_enabled()) ? palette().button_text() : palette().threed_shadow1());
  205. }
  206. if (has_scrubber())
  207. Gfx::StylePainter::paint_button(painter, scrubber_rect(), palette(), Gfx::ButtonStyle::Normal, false, hovered_component_for_painting == Component::Scrubber || m_pressed_component == Component::Scrubber);
  208. }
  209. void ScrollBar::on_automatic_scrolling_timer_fired()
  210. {
  211. if (m_pressed_component == Component::DecrementButton && component_at_position(m_last_mouse_position) == Component::DecrementButton) {
  212. set_value(value() - step());
  213. return;
  214. }
  215. if (m_pressed_component == Component::IncrementButton && component_at_position(m_last_mouse_position) == Component::IncrementButton) {
  216. set_value(value() + step());
  217. return;
  218. }
  219. if (m_pressed_component == Component::Gutter && component_at_position(m_last_mouse_position) == Component::Gutter) {
  220. scroll_by_page(m_last_mouse_position);
  221. m_hovered_component = component_at_position(m_last_mouse_position);
  222. return;
  223. }
  224. }
  225. void ScrollBar::mousedown_event(MouseEvent& event)
  226. {
  227. if (event.button() != MouseButton::Left)
  228. return;
  229. if (!has_scrubber())
  230. return;
  231. m_last_mouse_position = event.position();
  232. m_pressed_component = component_at_position(m_last_mouse_position);
  233. if (m_pressed_component == Component::DecrementButton) {
  234. set_automatic_scrolling_active(true, Component::DecrementButton);
  235. update();
  236. return;
  237. }
  238. if (m_pressed_component == Component::IncrementButton) {
  239. set_automatic_scrolling_active(true, Component::IncrementButton);
  240. update();
  241. return;
  242. }
  243. if (event.shift()) {
  244. scroll_to_position(event.position());
  245. m_pressed_component = component_at_position(event.position());
  246. VERIFY(m_pressed_component == Component::Scrubber);
  247. }
  248. if (m_pressed_component == Component::Scrubber) {
  249. m_scrub_start_value = value();
  250. m_scrub_origin = event.position();
  251. update();
  252. return;
  253. }
  254. VERIFY(!event.shift());
  255. VERIFY(m_pressed_component == Component::Gutter);
  256. set_automatic_scrolling_active(true, Component::Gutter);
  257. update();
  258. }
  259. void ScrollBar::mouseup_event(MouseEvent& event)
  260. {
  261. if (event.button() != MouseButton::Left)
  262. return;
  263. set_automatic_scrolling_active(false, Component::None);
  264. update();
  265. }
  266. void ScrollBar::mousewheel_event(MouseEvent& event)
  267. {
  268. if (!is_scrollable())
  269. return;
  270. set_value(value() + event.wheel_delta() * step());
  271. Widget::mousewheel_event(event);
  272. }
  273. void ScrollBar::set_automatic_scrolling_active(bool active, Component pressed_component)
  274. {
  275. m_pressed_component = pressed_component;
  276. if (m_pressed_component == Component::Gutter)
  277. m_automatic_scrolling_timer->set_interval(200);
  278. else
  279. m_automatic_scrolling_timer->set_interval(100);
  280. if (active) {
  281. on_automatic_scrolling_timer_fired();
  282. m_automatic_scrolling_timer->start();
  283. } else {
  284. m_automatic_scrolling_timer->stop();
  285. }
  286. }
  287. void ScrollBar::scroll_by_page(const Gfx::IntPoint& click_position)
  288. {
  289. float range_size = max() - min();
  290. float available = scrubbable_range_in_pixels();
  291. float rel_scrubber_size = unclamped_scrubber_size() / available;
  292. float page_increment = range_size * rel_scrubber_size;
  293. if (click_position.primary_offset_for_orientation(orientation()) < scrubber_rect().primary_offset_for_orientation(orientation()))
  294. set_value(value() - page_increment);
  295. else
  296. set_value(value() + page_increment);
  297. }
  298. void ScrollBar::scroll_to_position(const Gfx::IntPoint& click_position)
  299. {
  300. float range_size = max() - min();
  301. float available = scrubbable_range_in_pixels();
  302. float x_or_y = ::max(0, click_position.primary_offset_for_orientation(orientation()) - button_width() - button_width() / 2);
  303. float rel_x_or_y = x_or_y / available;
  304. set_value(min() + rel_x_or_y * range_size);
  305. }
  306. ScrollBar::Component ScrollBar::component_at_position(const Gfx::IntPoint& position)
  307. {
  308. if (scrubber_rect().contains(position))
  309. return Component::Scrubber;
  310. if (decrement_button_rect().contains(position))
  311. return Component::DecrementButton;
  312. if (increment_button_rect().contains(position))
  313. return Component::IncrementButton;
  314. if (rect().contains(position))
  315. return Component::Gutter;
  316. return Component::None;
  317. }
  318. void ScrollBar::mousemove_event(MouseEvent& event)
  319. {
  320. m_last_mouse_position = event.position();
  321. auto old_hovered_component = m_hovered_component;
  322. m_hovered_component = component_at_position(m_last_mouse_position);
  323. if (old_hovered_component != m_hovered_component) {
  324. update();
  325. }
  326. if (m_pressed_component != Component::Scrubber)
  327. return;
  328. float delta = orientation() == Orientation::Vertical ? (event.y() - m_scrub_origin.y()) : (event.x() - m_scrub_origin.x());
  329. float scrubbable_range = scrubbable_range_in_pixels();
  330. float value_steps_per_scrubbed_pixel = (max() - min()) / scrubbable_range;
  331. float new_value = m_scrub_start_value + (value_steps_per_scrubbed_pixel * delta);
  332. set_value(new_value);
  333. }
  334. void ScrollBar::leave_event(Core::Event&)
  335. {
  336. if (m_hovered_component != Component::None) {
  337. m_hovered_component = Component::None;
  338. update();
  339. }
  340. }
  341. void ScrollBar::change_event(Event& event)
  342. {
  343. if (event.type() == Event::Type::EnabledChange) {
  344. if (!is_enabled())
  345. set_automatic_scrolling_active(false, Component::None);
  346. }
  347. return Widget::change_event(event);
  348. }
  349. }