GScrollBar.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 <LibGfx/CharacterBitmap.h>
  27. #include <LibGfx/Bitmap.h>
  28. #include <LibGfx/Palette.h>
  29. #include <LibGfx/StylePainter.h>
  30. #include <LibGUI/GPainter.h>
  31. #include <LibGUI/GScrollBar.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(Widget* parent)
  82. : ScrollBar(Orientation::Vertical, parent)
  83. {
  84. }
  85. ScrollBar::ScrollBar(Orientation orientation, Widget* parent)
  86. : Widget(parent)
  87. , m_orientation(orientation)
  88. {
  89. m_automatic_scrolling_timer = Core::Timer::construct(this);
  90. if (!s_up_arrow_bitmap)
  91. s_up_arrow_bitmap = &Gfx::CharacterBitmap::create_from_ascii(s_up_arrow_bitmap_data, 9, 9).leak_ref();
  92. if (!s_down_arrow_bitmap)
  93. s_down_arrow_bitmap = &Gfx::CharacterBitmap::create_from_ascii(s_down_arrow_bitmap_data, 9, 9).leak_ref();
  94. if (!s_left_arrow_bitmap)
  95. s_left_arrow_bitmap = &Gfx::CharacterBitmap::create_from_ascii(s_left_arrow_bitmap_data, 9, 9).leak_ref();
  96. if (!s_right_arrow_bitmap)
  97. s_right_arrow_bitmap = &Gfx::CharacterBitmap::create_from_ascii(s_right_arrow_bitmap_data, 9, 9).leak_ref();
  98. if (m_orientation == Orientation::Vertical) {
  99. set_preferred_size(15, 0);
  100. } else {
  101. set_preferred_size(0, 15);
  102. }
  103. m_automatic_scrolling_timer->set_interval(100);
  104. m_automatic_scrolling_timer->on_timeout = [this] {
  105. on_automatic_scrolling_timer_fired();
  106. };
  107. }
  108. ScrollBar::~ScrollBar()
  109. {
  110. }
  111. void ScrollBar::set_range(int min, int max)
  112. {
  113. ASSERT(min <= max);
  114. if (m_min == min && m_max == max)
  115. return;
  116. m_min = min;
  117. m_max = max;
  118. int old_value = m_value;
  119. m_value = clamp(m_value, m_min, m_max);
  120. if (on_change && m_value != old_value)
  121. on_change(m_value);
  122. update();
  123. }
  124. void ScrollBar::set_value(int value)
  125. {
  126. value = clamp(value, m_min, m_max);
  127. if (value == m_value)
  128. return;
  129. m_value = value;
  130. if (on_change)
  131. on_change(value);
  132. update();
  133. }
  134. Rect ScrollBar::decrement_button_rect() const
  135. {
  136. return { 0, 0, button_width(), button_height() };
  137. }
  138. Rect ScrollBar::increment_button_rect() const
  139. {
  140. if (orientation() == Orientation::Vertical)
  141. return { 0, height() - button_height(), button_width(), button_height() };
  142. else
  143. return { width() - button_width(), 0, button_width(), button_height() };
  144. }
  145. Rect ScrollBar::decrement_gutter_rect() const
  146. {
  147. if (orientation() == Orientation::Vertical)
  148. return { 0, button_height(), button_width(), scrubber_rect().top() - button_height() };
  149. else
  150. return { button_width(), 0, scrubber_rect().x() - button_width(), button_height() };
  151. }
  152. Rect ScrollBar::increment_gutter_rect() const
  153. {
  154. auto scrubber_rect = this->scrubber_rect();
  155. if (orientation() == Orientation::Vertical)
  156. return { 0, scrubber_rect.bottom() + 1, button_width(), height() - button_height() - scrubber_rect.bottom() - 1 };
  157. else
  158. return { scrubber_rect.right() + 1, 0, width() - button_width() - scrubber_rect.right() - 1, button_width() };
  159. }
  160. int ScrollBar::scrubbable_range_in_pixels() const
  161. {
  162. if (orientation() == Orientation::Vertical)
  163. return height() - button_height() * 2 - scrubber_size();
  164. else
  165. return width() - button_width() * 2 - scrubber_size();
  166. }
  167. bool ScrollBar::has_scrubber() const
  168. {
  169. return m_max != m_min;
  170. }
  171. int ScrollBar::scrubber_size() const
  172. {
  173. int pixel_range = length(orientation()) - button_size() * 2;
  174. int value_range = m_max - m_min;
  175. return ::max(pixel_range - value_range, button_size());
  176. }
  177. Rect ScrollBar::scrubber_rect() const
  178. {
  179. if (!has_scrubber() || length(orientation()) <= (button_size() * 2) + scrubber_size())
  180. return {};
  181. float x_or_y;
  182. if (m_value == m_min)
  183. x_or_y = button_size();
  184. else if (m_value == m_max)
  185. x_or_y = (length(orientation()) - button_size() - scrubber_size()) + 1;
  186. else {
  187. float range_size = m_max - m_min;
  188. float available = scrubbable_range_in_pixels();
  189. float step = available / range_size;
  190. x_or_y = (button_size() + (step * m_value));
  191. }
  192. if (orientation() == Orientation::Vertical)
  193. return { 0, (int)x_or_y, button_width(), scrubber_size() };
  194. else
  195. return { (int)x_or_y, 0, scrubber_size(), button_height() };
  196. }
  197. void ScrollBar::paint_event(PaintEvent& event)
  198. {
  199. Painter painter(*this);
  200. painter.add_clip_rect(event.rect());
  201. painter.fill_rect(rect(), palette().button().lightened());
  202. Gfx::StylePainter::paint_button(painter, decrement_button_rect(), palette(), Gfx::ButtonStyle::Normal, false, m_hovered_component == Component::DecrementButton);
  203. Gfx::StylePainter::paint_button(painter, increment_button_rect(), palette(), Gfx::ButtonStyle::Normal, false, m_hovered_component == Component::IncrementButton);
  204. if (length(orientation()) > default_button_size()) {
  205. painter.draw_bitmap(decrement_button_rect().location().translated(3, 3), orientation() == Orientation::Vertical ? *s_up_arrow_bitmap : *s_left_arrow_bitmap, has_scrubber() ? palette().button_text() : palette().threed_shadow1());
  206. painter.draw_bitmap(increment_button_rect().location().translated(3, 3), orientation() == Orientation::Vertical ? *s_down_arrow_bitmap : *s_right_arrow_bitmap, has_scrubber() ? palette().button_text() : palette().threed_shadow1());
  207. }
  208. if (has_scrubber())
  209. Gfx::StylePainter::paint_button(painter, scrubber_rect(), palette(), Gfx::ButtonStyle::Normal, false, m_hovered_component == Component::Scrubber || m_scrubber_in_use);
  210. }
  211. void ScrollBar::on_automatic_scrolling_timer_fired()
  212. {
  213. if (m_automatic_scrolling_direction == AutomaticScrollingDirection::Decrement) {
  214. set_value(value() - m_step);
  215. return;
  216. }
  217. if (m_automatic_scrolling_direction == AutomaticScrollingDirection::Increment) {
  218. set_value(value() + m_step);
  219. return;
  220. }
  221. }
  222. void ScrollBar::mousedown_event(MouseEvent& event)
  223. {
  224. if (event.button() != MouseButton::Left)
  225. return;
  226. if (decrement_button_rect().contains(event.position())) {
  227. m_automatic_scrolling_direction = AutomaticScrollingDirection::Decrement;
  228. set_automatic_scrolling_active(true);
  229. return;
  230. }
  231. if (increment_button_rect().contains(event.position())) {
  232. m_automatic_scrolling_direction = AutomaticScrollingDirection::Increment;
  233. set_automatic_scrolling_active(true);
  234. return;
  235. }
  236. if (has_scrubber() && scrubber_rect().contains(event.position())) {
  237. m_scrubber_in_use = true;
  238. m_scrubbing = true;
  239. m_scrub_start_value = value();
  240. m_scrub_origin = event.position();
  241. update();
  242. return;
  243. }
  244. if (has_scrubber()) {
  245. float range_size = m_max - m_min;
  246. float available = scrubbable_range_in_pixels();
  247. float x = ::max(0, event.position().x() - button_width() - button_width() / 2);
  248. float y = ::max(0, event.position().y() - button_height() - button_height() / 2);
  249. float rel_x = x / available;
  250. float rel_y = y / available;
  251. if (orientation() == Orientation::Vertical)
  252. set_value(m_min + rel_y * range_size);
  253. else
  254. set_value(m_min + rel_x * range_size);
  255. m_scrubbing = true;
  256. m_scrub_start_value = value();
  257. m_scrub_origin = event.position();
  258. }
  259. }
  260. void ScrollBar::mouseup_event(MouseEvent& event)
  261. {
  262. if (event.button() != MouseButton::Left)
  263. return;
  264. m_scrubber_in_use = false;
  265. m_automatic_scrolling_direction = AutomaticScrollingDirection::None;
  266. set_automatic_scrolling_active(false);
  267. if (!m_scrubbing)
  268. return;
  269. m_scrubbing = false;
  270. update();
  271. }
  272. void ScrollBar::mousewheel_event(MouseEvent& event)
  273. {
  274. if (!is_scrollable())
  275. return;
  276. set_value(value() + event.wheel_delta() * m_step);
  277. Widget::mousewheel_event(event);
  278. }
  279. void ScrollBar::set_automatic_scrolling_active(bool active)
  280. {
  281. if (active) {
  282. on_automatic_scrolling_timer_fired();
  283. m_automatic_scrolling_timer->start();
  284. } else {
  285. m_automatic_scrolling_timer->stop();
  286. }
  287. }
  288. void ScrollBar::mousemove_event(MouseEvent& event)
  289. {
  290. auto old_hovered_component = m_hovered_component;
  291. if (scrubber_rect().contains(event.position()))
  292. m_hovered_component = Component::Scrubber;
  293. else if (decrement_button_rect().contains(event.position()))
  294. m_hovered_component = Component::DecrementButton;
  295. else if (increment_button_rect().contains(event.position()))
  296. m_hovered_component = Component::IncrementButton;
  297. else if (rect().contains(event.position()))
  298. m_hovered_component = Component::Gutter;
  299. else
  300. m_hovered_component = Component::Invalid;
  301. if (old_hovered_component != m_hovered_component) {
  302. update();
  303. if (m_automatic_scrolling_direction == AutomaticScrollingDirection::Decrement)
  304. set_automatic_scrolling_active(m_hovered_component == Component::DecrementButton);
  305. else if (m_automatic_scrolling_direction == AutomaticScrollingDirection::Increment)
  306. set_automatic_scrolling_active(m_hovered_component == Component::IncrementButton);
  307. }
  308. if (!m_scrubbing)
  309. return;
  310. float delta = orientation() == Orientation::Vertical ? (event.y() - m_scrub_origin.y()) : (event.x() - m_scrub_origin.x());
  311. float scrubbable_range = scrubbable_range_in_pixels();
  312. float value_steps_per_scrubbed_pixel = (m_max - m_min) / scrubbable_range;
  313. float new_value = m_scrub_start_value + (value_steps_per_scrubbed_pixel * delta);
  314. set_value(new_value);
  315. }
  316. void ScrollBar::leave_event(Core::Event&)
  317. {
  318. if (m_hovered_component != Component::Invalid) {
  319. m_hovered_component = Component::Invalid;
  320. update();
  321. }
  322. }
  323. void ScrollBar::change_event(Event& event)
  324. {
  325. if (event.type() == Event::Type::EnabledChange) {
  326. if (!is_enabled())
  327. m_scrubbing = false;
  328. }
  329. return Widget::change_event(event);
  330. }
  331. }