| 
									
										
										
										
											2020-09-11 18:17:39 +02:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2022-03-10 14:02:25 +01:00
										 |  |  |  * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org> | 
					
						
							| 
									
										
										
										
											2020-09-11 18:17:39 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-09-11 18:17:39 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <LibGUI/Event.h>
 | 
					
						
							|  |  |  | #include <LibGfx/Font.h>
 | 
					
						
							| 
									
										
										
										
											2021-02-10 08:25:35 +01:00
										 |  |  | #include <LibGfx/Painter.h>
 | 
					
						
							| 
									
										
										
										
											2020-09-11 18:17:39 +02:00
										 |  |  | #include <LibGfx/StylePainter.h>
 | 
					
						
							| 
									
										
										
										
											2021-11-18 15:01:28 +01:00
										 |  |  | #include <LibWeb/HTML/BrowsingContext.h>
 | 
					
						
							| 
									
										
										
										
											2022-02-15 19:16:57 +01:00
										 |  |  | #include <LibWeb/HTML/HTMLInputElement.h>
 | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  | #include <LibWeb/Layout/CheckBox.h>
 | 
					
						
							| 
									
										
										
										
											2021-04-04 11:51:36 -04:00
										 |  |  | #include <LibWeb/Layout/Label.h>
 | 
					
						
							| 
									
										
										
										
											2022-03-10 14:02:25 +01:00
										 |  |  | #include <LibWeb/Painting/CheckBoxPaintable.h>
 | 
					
						
							| 
									
										
										
										
											2020-09-11 18:17:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  | namespace Web::Layout { | 
					
						
							| 
									
										
										
										
											2020-09-11 18:17:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  | CheckBox::CheckBox(DOM::Document& document, HTML::HTMLInputElement& element, NonnullRefPtr<CSS::StyleProperties> style) | 
					
						
							| 
									
										
										
										
											2021-04-04 11:51:36 -04:00
										 |  |  |     : LabelableNode(document, element, move(style)) | 
					
						
							| 
									
										
										
										
											2020-09-11 18:17:39 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     set_intrinsic_width(13); | 
					
						
							|  |  |  |     set_intrinsic_height(13); | 
					
						
							| 
									
										
										
										
											2020-11-22 13:38:18 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  | CheckBox::~CheckBox() | 
					
						
							| 
									
										
										
										
											2020-11-22 13:38:18 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-09-11 18:17:39 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  | void CheckBox::handle_mousedown(Badge<EventHandler>, const Gfx::IntPoint&, unsigned button, unsigned) | 
					
						
							| 
									
										
										
										
											2020-09-11 18:17:39 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-10-27 13:20:27 +02:00
										 |  |  |     if (button != GUI::MouseButton::Primary || !dom_node().enabled()) | 
					
						
							| 
									
										
										
										
											2020-09-11 18:17:39 +02:00
										 |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     m_being_pressed = true; | 
					
						
							|  |  |  |     set_needs_display(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     m_tracking_mouse = true; | 
					
						
							| 
									
										
										
										
											2021-05-30 12:36:53 +02:00
										 |  |  |     browsing_context().event_handler().set_mouse_event_tracking_layout_node(this); | 
					
						
							| 
									
										
										
										
											2020-09-11 18:17:39 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  | void CheckBox::handle_mouseup(Badge<EventHandler>, const Gfx::IntPoint& position, unsigned button, unsigned) | 
					
						
							| 
									
										
										
										
											2020-09-11 18:17:39 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-10-27 13:20:27 +02:00
										 |  |  |     if (!m_tracking_mouse || button != GUI::MouseButton::Primary || !dom_node().enabled()) | 
					
						
							| 
									
										
										
										
											2020-09-11 18:17:39 +02:00
										 |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-11 18:37:18 +02:00
										 |  |  |     // NOTE: Changing the checked state of the DOM node may run arbitrary JS, which could disappear this node.
 | 
					
						
							|  |  |  |     NonnullRefPtr protect = *this; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-10 15:50:16 +01:00
										 |  |  |     bool is_inside_node_or_label = enclosing_int_rect(paint_box()->absolute_rect()).contains(position); | 
					
						
							| 
									
										
										
										
											2021-04-04 11:51:36 -04:00
										 |  |  |     if (!is_inside_node_or_label) | 
					
						
							|  |  |  |         is_inside_node_or_label = Label::is_inside_associated_label(*this, position); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-17 13:31:09 +01:00
										 |  |  |     if (is_inside_node_or_label) { | 
					
						
							|  |  |  |         dom_node().did_click_checkbox({}); | 
					
						
							| 
									
										
										
										
											2022-02-15 19:16:57 +01:00
										 |  |  |         dom_node().set_checked(!dom_node().checked(), HTML::HTMLInputElement::ChangeSource::User); | 
					
						
							| 
									
										
										
										
											2022-02-17 13:31:09 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-09-11 18:17:39 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     m_being_pressed = false; | 
					
						
							|  |  |  |     m_tracking_mouse = false; | 
					
						
							| 
									
										
										
										
											2021-05-30 12:36:53 +02:00
										 |  |  |     browsing_context().event_handler().set_mouse_event_tracking_layout_node(nullptr); | 
					
						
							| 
									
										
										
										
											2020-09-11 18:17:39 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  | void CheckBox::handle_mousemove(Badge<EventHandler>, const Gfx::IntPoint& position, unsigned, unsigned) | 
					
						
							| 
									
										
										
										
											2020-09-11 18:17:39 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-11-22 14:46:36 +01:00
										 |  |  |     if (!m_tracking_mouse || !dom_node().enabled()) | 
					
						
							| 
									
										
										
										
											2020-09-11 18:17:39 +02:00
										 |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-10 15:50:16 +01:00
										 |  |  |     bool is_inside_node_or_label = enclosing_int_rect(paint_box()->absolute_rect()).contains(position); | 
					
						
							| 
									
										
										
										
											2021-04-04 11:51:36 -04:00
										 |  |  |     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 CheckBox::handle_associated_label_mousedown(Badge<Label>) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-02-08 17:38:16 +01:00
										 |  |  |     if (!dom_node().enabled()) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-04 11:51:36 -04:00
										 |  |  |     m_being_pressed = true; | 
					
						
							|  |  |  |     set_needs_display(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void CheckBox::handle_associated_label_mouseup(Badge<Label>) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-02-08 17:38:16 +01:00
										 |  |  |     if (!dom_node().enabled()) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-04 11:51:36 -04:00
										 |  |  |     // NOTE: Changing the checked state of the DOM node may run arbitrary JS, which could disappear this node.
 | 
					
						
							|  |  |  |     NonnullRefPtr protect = *this; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-17 13:31:09 +01:00
										 |  |  |     dom_node().did_click_checkbox({}); | 
					
						
							| 
									
										
										
										
											2022-02-15 19:16:57 +01:00
										 |  |  |     dom_node().set_checked(!dom_node().checked(), HTML::HTMLInputElement::ChangeSource::User); | 
					
						
							| 
									
										
										
										
											2021-04-04 11:51:36 -04:00
										 |  |  |     m_being_pressed = false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void CheckBox::handle_associated_label_mousemove(Badge<Label>, bool is_inside_node_or_label) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-02-08 17:38:16 +01:00
										 |  |  |     if (m_being_pressed == is_inside_node_or_label || !dom_node().enabled()) | 
					
						
							| 
									
										
										
										
											2020-09-11 18:17:39 +02:00
										 |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-04 11:51:36 -04:00
										 |  |  |     m_being_pressed = is_inside_node_or_label; | 
					
						
							| 
									
										
										
										
											2020-09-11 18:17:39 +02:00
										 |  |  |     set_needs_display(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-10 22:38:08 +01:00
										 |  |  | RefPtr<Painting::Paintable> CheckBox::create_paintable() const | 
					
						
							| 
									
										
										
										
											2022-03-10 14:02:25 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     return Painting::CheckBoxPaintable::create(*this); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-11 18:17:39 +02:00
										 |  |  | } |