| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * All rights reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Redistribution and use in source and binary forms, with or without | 
					
						
							|  |  |  |  * modification, are permitted provided that the following conditions are met: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 1. Redistributions of source code must retain the above copyright notice, this | 
					
						
							|  |  |  |  *    list of conditions and the following disclaimer. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 2. Redistributions in binary form must reproduce the above copyright notice, | 
					
						
							|  |  |  |  *    this list of conditions and the following disclaimer in the documentation | 
					
						
							|  |  |  |  *    and/or other materials provided with the distribution. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | 
					
						
							|  |  |  |  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
					
						
							|  |  |  |  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 
					
						
							|  |  |  |  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | 
					
						
							|  |  |  |  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 
					
						
							|  |  |  |  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | 
					
						
							|  |  |  |  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | 
					
						
							|  |  |  |  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 
					
						
							|  |  |  |  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
					
						
							|  |  |  |  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-14 21:41:10 +01:00
										 |  |  | #include <AK/Assertions.h>
 | 
					
						
							|  |  |  | #include <AK/StdLibExtras.h>
 | 
					
						
							|  |  |  | #include <LibGUI/Painter.h>
 | 
					
						
							|  |  |  | #include <LibGUI/ScrollBar.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-06 12:07:05 +01:00
										 |  |  | #include <LibGfx/Bitmap.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-14 21:41:10 +01:00
										 |  |  | #include <LibGfx/CharacterBitmap.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-06 12:04:00 +01:00
										 |  |  | #include <LibGfx/Palette.h>
 | 
					
						
							|  |  |  | #include <LibGfx/StylePainter.h>
 | 
					
						
							| 
									
										
										
										
											2019-02-09 11:19:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | namespace GUI { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-10 08:23:03 +01:00
										 |  |  | static const char* s_up_arrow_bitmap_data = { | 
					
						
							| 
									
										
										
										
											2019-02-10 11:57:19 +01:00
										 |  |  |     "         " | 
					
						
							|  |  |  |     "    #    " | 
					
						
							|  |  |  |     "   ###   " | 
					
						
							|  |  |  |     "  #####  " | 
					
						
							|  |  |  |     " ####### " | 
					
						
							|  |  |  |     "   ###   " | 
					
						
							|  |  |  |     "   ###   " | 
					
						
							|  |  |  |     "   ###   " | 
					
						
							|  |  |  |     "         " | 
					
						
							| 
									
										
										
										
											2019-02-10 08:23:03 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static const char* s_down_arrow_bitmap_data = { | 
					
						
							| 
									
										
										
										
											2019-02-10 11:57:19 +01:00
										 |  |  |     "         " | 
					
						
							|  |  |  |     "   ###   " | 
					
						
							|  |  |  |     "   ###   " | 
					
						
							|  |  |  |     "   ###   " | 
					
						
							|  |  |  |     " ####### " | 
					
						
							|  |  |  |     "  #####  " | 
					
						
							|  |  |  |     "   ###   " | 
					
						
							|  |  |  |     "    #    " | 
					
						
							|  |  |  |     "         " | 
					
						
							| 
									
										
										
										
											2019-02-10 08:23:03 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-10 12:26:58 +01:00
										 |  |  | static const char* s_left_arrow_bitmap_data = { | 
					
						
							|  |  |  |     "         " | 
					
						
							|  |  |  |     "    #    " | 
					
						
							|  |  |  |     "   ##    " | 
					
						
							|  |  |  |     "  ###### " | 
					
						
							|  |  |  |     " ####### " | 
					
						
							|  |  |  |     "  ###### " | 
					
						
							|  |  |  |     "   ##    " | 
					
						
							|  |  |  |     "    #    " | 
					
						
							|  |  |  |     "         " | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static const char* s_right_arrow_bitmap_data = { | 
					
						
							|  |  |  |     "         " | 
					
						
							|  |  |  |     "    #    " | 
					
						
							|  |  |  |     "    ##   " | 
					
						
							|  |  |  |     " ######  " | 
					
						
							|  |  |  |     " ####### " | 
					
						
							|  |  |  |     " ######  " | 
					
						
							|  |  |  |     "    ##   " | 
					
						
							|  |  |  |     "    #    " | 
					
						
							|  |  |  |     "         " | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  | static Gfx::CharacterBitmap* s_up_arrow_bitmap; | 
					
						
							|  |  |  | static Gfx::CharacterBitmap* s_down_arrow_bitmap; | 
					
						
							|  |  |  | static Gfx::CharacterBitmap* s_left_arrow_bitmap; | 
					
						
							|  |  |  | static Gfx::CharacterBitmap* s_right_arrow_bitmap; | 
					
						
							| 
									
										
										
										
											2019-02-10 08:23:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | ScrollBar::ScrollBar(Widget* parent) | 
					
						
							|  |  |  |     : ScrollBar(Orientation::Vertical, parent) | 
					
						
							| 
									
										
										
										
											2019-11-10 10:58:03 +01:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | ScrollBar::ScrollBar(Orientation orientation, Widget* parent) | 
					
						
							|  |  |  |     : Widget(parent) | 
					
						
							| 
									
										
										
										
											2019-02-10 11:57:19 +01:00
										 |  |  |     , m_orientation(orientation) | 
					
						
							| 
									
										
										
										
											2019-02-09 11:19:38 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  |     m_automatic_scrolling_timer = Core::Timer::construct(this); | 
					
						
							| 
									
										
										
										
											2019-02-10 08:23:03 +01:00
										 |  |  |     if (!s_up_arrow_bitmap) | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  |         s_up_arrow_bitmap = &Gfx::CharacterBitmap::create_from_ascii(s_up_arrow_bitmap_data, 9, 9).leak_ref(); | 
					
						
							| 
									
										
										
										
											2019-02-10 08:23:03 +01:00
										 |  |  |     if (!s_down_arrow_bitmap) | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  |         s_down_arrow_bitmap = &Gfx::CharacterBitmap::create_from_ascii(s_down_arrow_bitmap_data, 9, 9).leak_ref(); | 
					
						
							| 
									
										
										
										
											2019-02-10 12:26:58 +01:00
										 |  |  |     if (!s_left_arrow_bitmap) | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  |         s_left_arrow_bitmap = &Gfx::CharacterBitmap::create_from_ascii(s_left_arrow_bitmap_data, 9, 9).leak_ref(); | 
					
						
							| 
									
										
										
										
											2019-02-10 12:26:58 +01:00
										 |  |  |     if (!s_right_arrow_bitmap) | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  |         s_right_arrow_bitmap = &Gfx::CharacterBitmap::create_from_ascii(s_right_arrow_bitmap_data, 9, 9).leak_ref(); | 
					
						
							| 
									
										
										
										
											2019-02-10 11:57:19 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (m_orientation == Orientation::Vertical) { | 
					
						
							| 
									
										
										
										
											2019-07-20 22:39:24 +02:00
										 |  |  |         set_preferred_size(15, 0); | 
					
						
							| 
									
										
										
										
											2019-02-10 11:57:19 +01:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2019-07-20 22:39:24 +02:00
										 |  |  |         set_preferred_size(0, 15); | 
					
						
							| 
									
										
										
										
											2019-02-10 11:57:19 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-06-07 10:43:10 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-20 15:19:46 +02:00
										 |  |  |     m_automatic_scrolling_timer->set_interval(100); | 
					
						
							|  |  |  |     m_automatic_scrolling_timer->on_timeout = [this] { | 
					
						
							| 
									
										
										
										
											2019-06-07 10:43:10 +02:00
										 |  |  |         on_automatic_scrolling_timer_fired(); | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2019-02-09 11:19:38 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | ScrollBar::~ScrollBar() | 
					
						
							| 
									
										
										
										
											2019-02-09 11:19:38 +01:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void ScrollBar::set_range(int min, int max) | 
					
						
							| 
									
										
										
										
											2019-02-09 11:19:38 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     ASSERT(min <= max); | 
					
						
							|  |  |  |     if (m_min == min && m_max == max) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     m_min = min; | 
					
						
							|  |  |  |     m_max = max; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     int old_value = m_value; | 
					
						
							| 
									
										
										
										
											2020-01-20 13:16:58 +01:00
										 |  |  |     m_value = clamp(m_value, m_min, m_max); | 
					
						
							| 
									
										
										
										
											2019-02-09 11:19:38 +01:00
										 |  |  |     if (on_change && m_value != old_value) | 
					
						
							|  |  |  |         on_change(m_value); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     update(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void ScrollBar::set_value(int value) | 
					
						
							| 
									
										
										
										
											2019-02-09 11:19:38 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-01-20 13:16:58 +01:00
										 |  |  |     value = clamp(value, m_min, m_max); | 
					
						
							| 
									
										
										
										
											2019-02-09 11:19:38 +01:00
										 |  |  |     if (value == m_value) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     m_value = value; | 
					
						
							|  |  |  |     if (on_change) | 
					
						
							|  |  |  |         on_change(value); | 
					
						
							|  |  |  |     update(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 13:02:38 +01:00
										 |  |  | Gfx::Rect ScrollBar::decrement_button_rect() const | 
					
						
							| 
									
										
										
										
											2019-02-09 11:19:38 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-11 13:16:43 +02:00
										 |  |  |     return { 0, 0, button_width(), button_height() }; | 
					
						
							| 
									
										
										
										
											2019-02-09 11:19:38 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 13:02:38 +01:00
										 |  |  | Gfx::Rect ScrollBar::increment_button_rect() const | 
					
						
							| 
									
										
										
										
											2019-02-09 11:19:38 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-02-10 12:26:58 +01:00
										 |  |  |     if (orientation() == Orientation::Vertical) | 
					
						
							| 
									
										
										
										
											2019-04-11 13:16:43 +02:00
										 |  |  |         return { 0, height() - button_height(), button_width(), button_height() }; | 
					
						
							| 
									
										
										
										
											2019-02-10 12:26:58 +01:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2019-04-11 13:16:43 +02:00
										 |  |  |         return { width() - button_width(), 0, button_width(), button_height() }; | 
					
						
							| 
									
										
										
										
											2019-02-09 11:19:38 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 13:02:38 +01:00
										 |  |  | Gfx::Rect ScrollBar::decrement_gutter_rect() const | 
					
						
							| 
									
										
										
										
											2019-02-09 11:19:38 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-02-10 12:26:58 +01:00
										 |  |  |     if (orientation() == Orientation::Vertical) | 
					
						
							| 
									
										
										
										
											2019-04-11 13:16:43 +02:00
										 |  |  |         return { 0, button_height(), button_width(), scrubber_rect().top() - button_height() }; | 
					
						
							| 
									
										
										
										
											2019-02-10 12:26:58 +01:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2019-04-11 13:16:43 +02:00
										 |  |  |         return { button_width(), 0, scrubber_rect().x() - button_width(), button_height() }; | 
					
						
							| 
									
										
										
										
											2019-02-09 11:19:38 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 13:02:38 +01:00
										 |  |  | Gfx::Rect ScrollBar::increment_gutter_rect() const | 
					
						
							| 
									
										
										
										
											2019-02-09 11:19:38 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     auto scrubber_rect = this->scrubber_rect(); | 
					
						
							| 
									
										
										
										
											2019-02-10 12:26:58 +01:00
										 |  |  |     if (orientation() == Orientation::Vertical) | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:02 +02:00
										 |  |  |         return { 0, scrubber_rect.bottom() + 1, button_width(), height() - button_height() - scrubber_rect.bottom() - 1 }; | 
					
						
							| 
									
										
										
										
											2019-02-10 12:26:58 +01:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2019-04-11 13:16:43 +02:00
										 |  |  |         return { scrubber_rect.right() + 1, 0, width() - button_width() - scrubber_rect.right() - 1, button_width() }; | 
					
						
							| 
									
										
										
										
											2019-02-09 11:19:38 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | int ScrollBar::scrubbable_range_in_pixels() const | 
					
						
							| 
									
										
										
										
											2019-02-10 06:51:01 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-02-10 12:26:58 +01:00
										 |  |  |     if (orientation() == Orientation::Vertical) | 
					
						
							| 
									
										
										
										
											2019-04-11 13:16:43 +02:00
										 |  |  |         return height() - button_height() * 2 - scrubber_size(); | 
					
						
							| 
									
										
										
										
											2019-02-10 12:26:58 +01:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2019-04-11 13:16:43 +02:00
										 |  |  |         return width() - button_width() * 2 - scrubber_size(); | 
					
						
							| 
									
										
										
										
											2019-02-10 06:51:01 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | bool ScrollBar::has_scrubber() const | 
					
						
							| 
									
										
										
										
											2019-02-10 07:11:01 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     return m_max != m_min; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | int ScrollBar::scrubber_size() const | 
					
						
							| 
									
										
										
										
											2019-03-29 02:51:19 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-07-01 02:46:36 -05:00
										 |  |  |     int pixel_range = length(orientation()) - button_size() * 2; | 
					
						
							| 
									
										
										
										
											2019-03-29 02:51:19 +01:00
										 |  |  |     int value_range = m_max - m_min; | 
					
						
							|  |  |  |     return ::max(pixel_range - value_range, button_size()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 13:02:38 +01:00
										 |  |  | Gfx::Rect ScrollBar::scrubber_rect() const | 
					
						
							| 
									
										
										
										
											2019-02-09 11:19:38 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-07-01 02:46:36 -05:00
										 |  |  |     if (!has_scrubber() || length(orientation()) <= (button_size() * 2) + scrubber_size()) | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:02 +02:00
										 |  |  |         return {}; | 
					
						
							| 
									
										
										
										
											2019-02-10 12:26:58 +01:00
										 |  |  |     float x_or_y; | 
					
						
							|  |  |  |     if (m_value == m_min) | 
					
						
							| 
									
										
										
										
											2019-03-27 20:48:23 +01:00
										 |  |  |         x_or_y = button_size(); | 
					
						
							| 
									
										
										
										
											2019-02-10 12:26:58 +01:00
										 |  |  |     else if (m_value == m_max) | 
					
						
							| 
									
										
										
										
											2019-07-01 02:46:36 -05:00
										 |  |  |         x_or_y = (length(orientation()) - button_size() - scrubber_size()) + 1; | 
					
						
							| 
									
										
										
										
											2019-02-10 12:26:58 +01:00
										 |  |  |     else { | 
					
						
							|  |  |  |         float range_size = m_max - m_min; | 
					
						
							|  |  |  |         float available = scrubbable_range_in_pixels(); | 
					
						
							|  |  |  |         float step = available / range_size; | 
					
						
							|  |  |  |         x_or_y = (button_size() + (step * m_value)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (orientation() == Orientation::Vertical) | 
					
						
							| 
									
										
										
										
											2019-04-11 13:16:43 +02:00
										 |  |  |         return { 0, (int)x_or_y, button_width(), scrubber_size() }; | 
					
						
							| 
									
										
										
										
											2019-02-10 12:26:58 +01:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2019-04-11 13:16:43 +02:00
										 |  |  |         return { (int)x_or_y, 0, scrubber_size(), button_height() }; | 
					
						
							| 
									
										
										
										
											2019-02-09 11:19:38 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void ScrollBar::paint_event(PaintEvent& event) | 
					
						
							| 
									
										
										
										
											2019-02-09 11:19:38 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     Painter painter(*this); | 
					
						
							| 
									
										
										
										
											2019-03-29 15:01:54 +01:00
										 |  |  |     painter.add_clip_rect(event.rect()); | 
					
						
							| 
									
										
										
										
											2019-02-09 11:19:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-24 20:57:54 +01:00
										 |  |  |     painter.fill_rect(rect(), palette().button().lightened()); | 
					
						
							| 
									
										
										
										
											2019-02-09 11:19:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 08:56:13 +01:00
										 |  |  |     bool decrement_pressed = m_automatic_scrolling_direction == AutomaticScrollingDirection::Decrement; | 
					
						
							|  |  |  |     bool increment_pressed = m_automatic_scrolling_direction == AutomaticScrollingDirection::Increment; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Gfx::StylePainter::paint_button(painter, decrement_button_rect(), palette(), Gfx::ButtonStyle::Normal, decrement_pressed, m_hovered_component == Component::DecrementButton); | 
					
						
							|  |  |  |     Gfx::StylePainter::paint_button(painter, increment_button_rect(), palette(), Gfx::ButtonStyle::Normal, increment_pressed, m_hovered_component == Component::IncrementButton); | 
					
						
							| 
									
										
										
										
											2019-07-01 02:46:36 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (length(orientation()) > default_button_size()) { | 
					
						
							| 
									
										
										
										
											2020-02-16 08:56:13 +01:00
										 |  |  |         auto decrement_location = decrement_button_rect().location().translated(3, 3); | 
					
						
							|  |  |  |         if (decrement_pressed) | 
					
						
							|  |  |  |             decrement_location.move_by(1, 1); | 
					
						
							|  |  |  |         painter.draw_bitmap(decrement_location, orientation() == Orientation::Vertical ? *s_up_arrow_bitmap : *s_left_arrow_bitmap, has_scrubber() ? palette().button_text() : palette().threed_shadow1()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         auto increment_location = increment_button_rect().location().translated(3, 3); | 
					
						
							|  |  |  |         if (increment_pressed) | 
					
						
							|  |  |  |             increment_location.move_by(1, 1); | 
					
						
							|  |  |  |         painter.draw_bitmap(increment_location, orientation() == Orientation::Vertical ? *s_down_arrow_bitmap : *s_right_arrow_bitmap, has_scrubber() ? palette().button_text() : palette().threed_shadow1()); | 
					
						
							| 
									
										
										
										
											2019-07-01 02:46:36 -05:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-02-09 11:19:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-10 07:11:01 +01:00
										 |  |  |     if (has_scrubber()) | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  |         Gfx::StylePainter::paint_button(painter, scrubber_rect(), palette(), Gfx::ButtonStyle::Normal, false, m_hovered_component == Component::Scrubber || m_scrubber_in_use); | 
					
						
							| 
									
										
										
										
											2019-02-09 11:19:38 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void ScrollBar::on_automatic_scrolling_timer_fired() | 
					
						
							| 
									
										
										
										
											2019-06-07 10:43:10 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     if (m_automatic_scrolling_direction == AutomaticScrollingDirection::Decrement) { | 
					
						
							|  |  |  |         set_value(value() - m_step); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (m_automatic_scrolling_direction == AutomaticScrollingDirection::Increment) { | 
					
						
							|  |  |  |         set_value(value() + m_step); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void ScrollBar::mousedown_event(MouseEvent& event) | 
					
						
							| 
									
										
										
										
											2019-02-09 11:19:38 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     if (event.button() != MouseButton::Left) | 
					
						
							| 
									
										
										
										
											2019-02-09 11:19:38 +01:00
										 |  |  |         return; | 
					
						
							| 
									
										
										
										
											2020-02-16 08:56:13 +01:00
										 |  |  |     if (!has_scrubber()) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-07 10:56:30 +02:00
										 |  |  |     if (decrement_button_rect().contains(event.position())) { | 
					
						
							| 
									
										
										
										
											2019-06-07 10:43:10 +02:00
										 |  |  |         m_automatic_scrolling_direction = AutomaticScrollingDirection::Decrement; | 
					
						
							|  |  |  |         set_automatic_scrolling_active(true); | 
					
						
							| 
									
										
										
										
											2020-02-16 08:56:13 +01:00
										 |  |  |         update(); | 
					
						
							| 
									
										
										
										
											2019-02-09 11:19:38 +01:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-06-07 10:56:30 +02:00
										 |  |  |     if (increment_button_rect().contains(event.position())) { | 
					
						
							| 
									
										
										
										
											2019-06-07 10:43:10 +02:00
										 |  |  |         m_automatic_scrolling_direction = AutomaticScrollingDirection::Increment; | 
					
						
							|  |  |  |         set_automatic_scrolling_active(true); | 
					
						
							| 
									
										
										
										
											2020-02-16 08:56:13 +01:00
										 |  |  |         update(); | 
					
						
							| 
									
										
										
										
											2019-02-09 11:19:38 +01:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-02-16 08:56:13 +01:00
										 |  |  |     if (scrubber_rect().contains(event.position())) { | 
					
						
							| 
									
										
										
										
											2019-11-10 10:58:03 +01:00
										 |  |  |         m_scrubber_in_use = true; | 
					
						
							| 
									
										
										
										
											2019-02-10 06:51:01 +01:00
										 |  |  |         m_scrubbing = true; | 
					
						
							|  |  |  |         m_scrub_start_value = value(); | 
					
						
							|  |  |  |         m_scrub_origin = event.position(); | 
					
						
							| 
									
										
										
										
											2019-02-10 07:11:01 +01:00
										 |  |  |         update(); | 
					
						
							| 
									
										
										
										
											2019-02-10 06:51:01 +01:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-06-07 10:56:30 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 08:56:13 +01:00
										 |  |  |     float range_size = m_max - m_min; | 
					
						
							|  |  |  |     float available = scrubbable_range_in_pixels(); | 
					
						
							| 
									
										
										
										
											2019-03-25 05:03:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 08:56:13 +01:00
										 |  |  |     float x = ::max(0, event.position().x() - button_width() - button_width() / 2); | 
					
						
							|  |  |  |     float y = ::max(0, event.position().y() - button_height() - button_height() / 2); | 
					
						
							| 
									
										
										
										
											2019-03-25 05:03:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 08:56:13 +01:00
										 |  |  |     float rel_x = x / available; | 
					
						
							|  |  |  |     float rel_y = y / available; | 
					
						
							| 
									
										
										
										
											2019-03-25 05:03:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 08:56:13 +01:00
										 |  |  |     if (orientation() == Orientation::Vertical) | 
					
						
							|  |  |  |         set_value(m_min + rel_y * range_size); | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |         set_value(m_min + rel_x * range_size); | 
					
						
							| 
									
										
										
										
											2019-03-25 05:03:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 08:56:13 +01:00
										 |  |  |     m_scrubbing = true; | 
					
						
							|  |  |  |     m_scrub_start_value = value(); | 
					
						
							|  |  |  |     m_scrub_origin = event.position(); | 
					
						
							| 
									
										
										
										
											2019-02-10 06:51:01 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void ScrollBar::mouseup_event(MouseEvent& event) | 
					
						
							| 
									
										
										
										
											2019-02-10 06:51:01 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     if (event.button() != MouseButton::Left) | 
					
						
							| 
									
										
										
										
											2019-02-10 06:51:01 +01:00
										 |  |  |         return; | 
					
						
							| 
									
										
										
										
											2019-08-11 12:53:02 +03:00
										 |  |  |     m_scrubber_in_use = false; | 
					
						
							| 
									
										
										
										
											2019-06-07 10:43:10 +02:00
										 |  |  |     m_automatic_scrolling_direction = AutomaticScrollingDirection::None; | 
					
						
							|  |  |  |     set_automatic_scrolling_active(false); | 
					
						
							| 
									
										
										
										
											2019-02-10 06:51:01 +01:00
										 |  |  |     m_scrubbing = false; | 
					
						
							| 
									
										
										
										
											2019-02-10 07:11:01 +01:00
										 |  |  |     update(); | 
					
						
							| 
									
										
										
										
											2019-02-10 06:51:01 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void ScrollBar::mousewheel_event(MouseEvent& event) | 
					
						
							| 
									
										
										
										
											2019-08-20 20:10:02 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     if (!is_scrollable()) | 
					
						
							|  |  |  |         return; | 
					
						
							| 
									
										
										
										
											2019-09-06 15:00:34 +02:00
										 |  |  |     set_value(value() + event.wheel_delta() * m_step); | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     Widget::mousewheel_event(event); | 
					
						
							| 
									
										
										
										
											2019-08-20 20:10:02 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void ScrollBar::set_automatic_scrolling_active(bool active) | 
					
						
							| 
									
										
										
										
											2019-06-07 10:43:10 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     if (active) { | 
					
						
							|  |  |  |         on_automatic_scrolling_timer_fired(); | 
					
						
							| 
									
										
										
										
											2019-09-20 15:19:46 +02:00
										 |  |  |         m_automatic_scrolling_timer->start(); | 
					
						
							| 
									
										
										
										
											2019-06-07 10:43:10 +02:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2019-09-20 15:19:46 +02:00
										 |  |  |         m_automatic_scrolling_timer->stop(); | 
					
						
							| 
									
										
										
										
											2019-06-07 10:43:10 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void ScrollBar::mousemove_event(MouseEvent& event) | 
					
						
							| 
									
										
										
										
											2019-02-10 06:51:01 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-06 13:55:56 +02:00
										 |  |  |     auto old_hovered_component = m_hovered_component; | 
					
						
							|  |  |  |     if (scrubber_rect().contains(event.position())) | 
					
						
							|  |  |  |         m_hovered_component = Component::Scrubber; | 
					
						
							| 
									
										
										
										
											2019-06-07 10:56:30 +02:00
										 |  |  |     else if (decrement_button_rect().contains(event.position())) | 
					
						
							| 
									
										
										
										
											2019-04-06 13:55:56 +02:00
										 |  |  |         m_hovered_component = Component::DecrementButton; | 
					
						
							| 
									
										
										
										
											2019-06-07 10:56:30 +02:00
										 |  |  |     else if (increment_button_rect().contains(event.position())) | 
					
						
							| 
									
										
										
										
											2019-04-06 13:55:56 +02:00
										 |  |  |         m_hovered_component = Component::IncrementButton; | 
					
						
							|  |  |  |     else if (rect().contains(event.position())) | 
					
						
							|  |  |  |         m_hovered_component = Component::Gutter; | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |         m_hovered_component = Component::Invalid; | 
					
						
							| 
									
										
										
										
											2019-06-07 10:43:10 +02:00
										 |  |  |     if (old_hovered_component != m_hovered_component) { | 
					
						
							| 
									
										
										
										
											2019-04-06 13:55:56 +02:00
										 |  |  |         update(); | 
					
						
							| 
									
										
										
										
											2019-06-07 10:43:10 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (m_automatic_scrolling_direction == AutomaticScrollingDirection::Decrement) | 
					
						
							|  |  |  |             set_automatic_scrolling_active(m_hovered_component == Component::DecrementButton); | 
					
						
							|  |  |  |         else if (m_automatic_scrolling_direction == AutomaticScrollingDirection::Increment) | 
					
						
							|  |  |  |             set_automatic_scrolling_active(m_hovered_component == Component::IncrementButton); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-02-10 06:51:01 +01:00
										 |  |  |     if (!m_scrubbing) | 
					
						
							|  |  |  |         return; | 
					
						
							| 
									
										
										
										
											2019-02-10 12:26:58 +01:00
										 |  |  |     float delta = orientation() == Orientation::Vertical ? (event.y() - m_scrub_origin.y()) : (event.x() - m_scrub_origin.x()); | 
					
						
							| 
									
										
										
										
											2019-02-10 06:51:01 +01:00
										 |  |  |     float scrubbable_range = scrubbable_range_in_pixels(); | 
					
						
							|  |  |  |     float value_steps_per_scrubbed_pixel = (m_max - m_min) / scrubbable_range; | 
					
						
							|  |  |  |     float new_value = m_scrub_start_value + (value_steps_per_scrubbed_pixel * delta); | 
					
						
							|  |  |  |     set_value(new_value); | 
					
						
							| 
									
										
										
										
											2019-02-09 11:19:38 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-04-06 13:55:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void ScrollBar::leave_event(Core::Event&) | 
					
						
							| 
									
										
										
										
											2019-04-06 13:55:56 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-10 01:50:10 +02:00
										 |  |  |     if (m_hovered_component != Component::Invalid) { | 
					
						
							|  |  |  |         m_hovered_component = Component::Invalid; | 
					
						
							|  |  |  |         update(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-04-06 13:55:56 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-05-25 13:40:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void ScrollBar::change_event(Event& event) | 
					
						
							| 
									
										
										
										
											2019-05-25 13:40:57 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     if (event.type() == Event::Type::EnabledChange) { | 
					
						
							| 
									
										
										
										
											2019-05-25 13:40:57 +02:00
										 |  |  |         if (!is_enabled()) | 
					
						
							|  |  |  |             m_scrubbing = false; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     return Widget::change_event(event); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-25 13:40:57 +02:00
										 |  |  | } |