ScrollBar.cpp 14 KB

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