| 
									
										
										
										
											2020-09-12 17:56:11 +02:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2020, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-09-12 17:56:11 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <LibGUI/Event.h>
 | 
					
						
							|  |  |  | #include <LibGfx/Font.h>
 | 
					
						
							| 
									
										
										
										
											2021-02-10 08:25:35 +01:00
										 |  |  | #include <LibGfx/Painter.h>
 | 
					
						
							| 
									
										
										
										
											2020-09-12 17:56:11 +02:00
										 |  |  | #include <LibGfx/StylePainter.h>
 | 
					
						
							|  |  |  | #include <LibWeb/DOM/Document.h>
 | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  | #include <LibWeb/Layout/ButtonBox.h>
 | 
					
						
							| 
									
										
										
										
											2021-04-04 12:17:24 -04:00
										 |  |  | #include <LibWeb/Layout/Label.h>
 | 
					
						
							| 
									
										
										
										
											2020-09-12 17:56:11 +02:00
										 |  |  | #include <LibWeb/Page/Frame.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  | namespace Web::Layout { | 
					
						
							| 
									
										
										
										
											2020-09-12 17:56:11 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  | ButtonBox::ButtonBox(DOM::Document& document, HTML::HTMLInputElement& element, NonnullRefPtr<CSS::StyleProperties> style) | 
					
						
							| 
									
										
										
										
											2021-04-04 12:17:24 -04:00
										 |  |  |     : LabelableNode(document, element, move(style)) | 
					
						
							| 
									
										
										
										
											2020-09-12 17:56:11 +02:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  | ButtonBox::~ButtonBox() | 
					
						
							| 
									
										
										
										
											2020-09-12 17:56:11 +02:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  | void ButtonBox::prepare_for_replaced_layout() | 
					
						
							| 
									
										
										
										
											2020-09-12 17:56:11 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-01-06 11:05:23 +01:00
										 |  |  |     set_intrinsic_width(font().width(dom_node().value()) + 20); | 
					
						
							| 
									
										
										
										
											2020-09-12 17:56:11 +02:00
										 |  |  |     set_has_intrinsic_width(true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     set_intrinsic_height(20); | 
					
						
							|  |  |  |     set_has_intrinsic_height(true); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  | void ButtonBox::paint(PaintContext& context, PaintPhase phase) | 
					
						
							| 
									
										
										
										
											2020-09-12 17:56:11 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     if (!is_visible()) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-04 12:17:24 -04:00
										 |  |  |     LabelableNode::paint(context, phase); | 
					
						
							| 
									
										
										
										
											2020-09-12 17:56:11 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (phase == PaintPhase::Foreground) { | 
					
						
							| 
									
										
										
										
											2020-11-22 14:46:36 +01:00
										 |  |  |         bool hovered = document().hovered_node() == &dom_node(); | 
					
						
							| 
									
										
										
										
											2021-04-04 12:17:24 -04:00
										 |  |  |         if (!hovered) | 
					
						
							|  |  |  |             hovered = Label::is_associated_label_hovered(*this); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 14:46:36 +01:00
										 |  |  |         Gfx::StylePainter::paint_button(context.painter(), enclosing_int_rect(absolute_rect()), context.palette(), Gfx::ButtonStyle::Normal, m_being_pressed, hovered, dom_node().checked(), dom_node().enabled()); | 
					
						
							| 
									
										
										
										
											2020-09-12 17:56:11 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         auto text_rect = enclosing_int_rect(absolute_rect()); | 
					
						
							|  |  |  |         if (m_being_pressed) | 
					
						
							| 
									
										
										
										
											2021-04-12 11:47:09 -07:00
										 |  |  |             text_rect.translate_by(1, 1); | 
					
						
							| 
									
										
										
										
											2021-01-06 11:05:23 +01:00
										 |  |  |         context.painter().draw_text(text_rect, dom_node().value(), font(), Gfx::TextAlignment::Center, context.palette().button_text()); | 
					
						
							| 
									
										
										
										
											2020-09-12 17:56:11 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  | void ButtonBox::handle_mousedown(Badge<EventHandler>, const Gfx::IntPoint&, unsigned button, unsigned) | 
					
						
							| 
									
										
										
										
											2020-09-12 17:56:11 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-11-22 14:46:36 +01:00
										 |  |  |     if (button != GUI::MouseButton::Left || !dom_node().enabled()) | 
					
						
							| 
									
										
										
										
											2020-09-12 17:56:11 +02:00
										 |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     m_being_pressed = true; | 
					
						
							|  |  |  |     set_needs_display(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     m_tracking_mouse = true; | 
					
						
							|  |  |  |     frame().event_handler().set_mouse_event_tracking_layout_node(this); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  | void ButtonBox::handle_mouseup(Badge<EventHandler>, const Gfx::IntPoint& position, unsigned button, unsigned) | 
					
						
							| 
									
										
										
										
											2020-09-12 17:56:11 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-11-22 14:46:36 +01:00
										 |  |  |     if (!m_tracking_mouse || button != GUI::MouseButton::Left || !dom_node().enabled()) | 
					
						
							| 
									
										
										
										
											2020-09-12 17:56:11 +02:00
										 |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // NOTE: Handling the click may run arbitrary JS, which could disappear this node.
 | 
					
						
							|  |  |  |     NonnullRefPtr protected_this = *this; | 
					
						
							|  |  |  |     NonnullRefPtr protected_frame = frame(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-04 12:17:24 -04:00
										 |  |  |     bool is_inside_node_or_label = enclosing_int_rect(absolute_rect()).contains(position); | 
					
						
							|  |  |  |     if (!is_inside_node_or_label) | 
					
						
							|  |  |  |         is_inside_node_or_label = Label::is_inside_associated_label(*this, position); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (is_inside_node_or_label) | 
					
						
							| 
									
										
										
										
											2020-11-22 14:46:36 +01:00
										 |  |  |         dom_node().did_click_button({}); | 
					
						
							| 
									
										
										
										
											2020-09-12 17:56:11 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     m_being_pressed = false; | 
					
						
							|  |  |  |     m_tracking_mouse = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     protected_frame->event_handler().set_mouse_event_tracking_layout_node(nullptr); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  | void ButtonBox::handle_mousemove(Badge<EventHandler>, const Gfx::IntPoint& position, unsigned, unsigned) | 
					
						
							| 
									
										
										
										
											2020-09-12 17:56:11 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-11-22 14:46:36 +01:00
										 |  |  |     if (!m_tracking_mouse || !dom_node().enabled()) | 
					
						
							| 
									
										
										
										
											2020-09-12 17:56:11 +02:00
										 |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-04 12:17:24 -04:00
										 |  |  |     bool is_inside_node_or_label = enclosing_int_rect(absolute_rect()).contains(position); | 
					
						
							|  |  |  |     if (!is_inside_node_or_label) | 
					
						
							|  |  |  |         is_inside_node_or_label = Label::is_inside_associated_label(*this, position); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (m_being_pressed == is_inside_node_or_label) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     m_being_pressed = is_inside_node_or_label; | 
					
						
							|  |  |  |     set_needs_display(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ButtonBox::handle_associated_label_mousedown(Badge<Label>) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_being_pressed = true; | 
					
						
							|  |  |  |     set_needs_display(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ButtonBox::handle_associated_label_mouseup(Badge<Label>) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // NOTE: Handling the click may run arbitrary JS, which could disappear this node.
 | 
					
						
							|  |  |  |     NonnullRefPtr protected_this = *this; | 
					
						
							|  |  |  |     NonnullRefPtr protected_frame = frame(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     dom_node().did_click_button({}); | 
					
						
							|  |  |  |     m_being_pressed = false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ButtonBox::handle_associated_label_mousemove(Badge<Label>, bool is_inside_node_or_label) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (m_being_pressed == is_inside_node_or_label) | 
					
						
							| 
									
										
										
										
											2020-09-12 17:56:11 +02:00
										 |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-04 12:17:24 -04:00
										 |  |  |     m_being_pressed = is_inside_node_or_label; | 
					
						
							| 
									
										
										
										
											2020-09-12 17:56:11 +02:00
										 |  |  |     set_needs_display(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |