GScrollBar.cpp 12 KB

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