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>
|
2021-11-18 15:01:28 +01:00
|
|
|
#include <LibWeb/HTML/BrowsingContext.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>
|
2022-03-10 11:26:01 +01:00
|
|
|
#include <LibWeb/Painting/Paintable.h>
|
2020-09-12 17:56:11 +02:00
|
|
|
|
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
|
|
|
{
|
2022-02-21 02:16:21 +01:00
|
|
|
set_intrinsic_width(font().width(dom_node().value()));
|
|
|
|
|
set_intrinsic_height(font().glyph_height());
|
2020-09-12 17:56:11 +02:00
|
|
|
}
|
|
|
|
|
|
2022-03-10 02:13:28 +01:00
|
|
|
void ButtonBox::paint(PaintContext& context, Painting::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
|
|
|
|
2022-03-10 02:13:28 +01:00
|
|
|
if (phase == Painting::PaintPhase::Foreground) {
|
2022-03-09 23:53:41 +01:00
|
|
|
auto text_rect = enclosing_int_rect(m_paint_box->absolute_rect());
|
2020-09-12 17:56:11 +02:00
|
|
|
if (m_being_pressed)
|
2021-04-12 11:47:09 -07:00
|
|
|
text_rect.translate_by(1, 1);
|
2022-02-21 02:16:21 +01:00
|
|
|
context.painter().draw_text(text_rect, dom_node().value(), font(), Gfx::TextAlignment::Center, computed_values().color());
|
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
|
|
|
{
|
2021-10-27 13:20:27 +02:00
|
|
|
if (button != GUI::MouseButton::Primary || !dom_node().enabled())
|
2020-09-12 17:56:11 +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-12 17:56:11 +02:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
{
|
2021-10-27 13:20:27 +02:00
|
|
|
if (!m_tracking_mouse || button != GUI::MouseButton::Primary || !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;
|
2022-02-06 14:41:29 +01:00
|
|
|
NonnullRefPtr protected_browsing_context = browsing_context();
|
2020-09-12 17:56:11 +02:00
|
|
|
|
2022-03-09 23:53:41 +01:00
|
|
|
bool is_inside_node_or_label = enclosing_int_rect(m_paint_box->absolute_rect()).contains(position);
|
2021-04-04 12:17:24 -04:00
|
|
|
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;
|
|
|
|
|
|
2022-02-06 14:41:29 +01:00
|
|
|
protected_browsing_context->event_handler().set_mouse_event_tracking_layout_node(nullptr);
|
2020-09-12 17:56:11 +02:00
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
|
2022-03-09 23:53:41 +01:00
|
|
|
bool is_inside_node_or_label = enclosing_int_rect(m_paint_box->absolute_rect()).contains(position);
|
2021-04-04 12:17:24 -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 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;
|
2022-02-06 14:41:29 +01:00
|
|
|
NonnullRefPtr protected_browsing_context = browsing_context();
|
2021-04-04 12:17:24 -04:00
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|