2020-09-11 18:17:39 +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-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>
|
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>
|
2021-05-30 12:36:53 +02:00
|
|
|
#include <LibWeb/Page/BrowsingContext.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::paint(PaintContext& context, PaintPhase phase)
|
2020-09-11 18:17:39 +02:00
|
|
|
{
|
|
|
|
if (!is_visible())
|
|
|
|
return;
|
|
|
|
|
2021-04-04 11:51:36 -04:00
|
|
|
LabelableNode::paint(context, phase);
|
2020-09-11 18:17:39 +02:00
|
|
|
|
|
|
|
if (phase == PaintPhase::Foreground) {
|
2020-11-22 14:46:36 +01:00
|
|
|
Gfx::StylePainter::paint_check_box(context.painter(), enclosing_int_rect(absolute_rect()), context.palette(), dom_node().enabled(), dom_node().checked(), m_being_pressed);
|
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;
|
|
|
|
|
2021-04-04 11:51:36 -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().set_checked(!dom_node().checked());
|
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;
|
|
|
|
|
2021-04-04 11:51:36 -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 CheckBox::handle_associated_label_mousedown(Badge<Label>)
|
|
|
|
{
|
|
|
|
m_being_pressed = true;
|
|
|
|
set_needs_display();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckBox::handle_associated_label_mouseup(Badge<Label>)
|
|
|
|
{
|
|
|
|
// NOTE: Changing the checked state of the DOM node may run arbitrary JS, which could disappear this node.
|
|
|
|
NonnullRefPtr protect = *this;
|
|
|
|
|
|
|
|
dom_node().set_checked(!dom_node().checked());
|
|
|
|
m_being_pressed = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckBox::handle_associated_label_mousemove(Badge<Label>, bool is_inside_node_or_label)
|
|
|
|
{
|
|
|
|
if (m_being_pressed == is_inside_node_or_label)
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|