/* * Copyright (c) 2020, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include #include #include #include #include #include #include namespace Web::Layout { ButtonBox::ButtonBox(DOM::Document& document, HTML::HTMLInputElement& element, NonnullRefPtr style) : LabelableNode(document, element, move(style)) { } ButtonBox::~ButtonBox() { } void ButtonBox::prepare_for_replaced_layout() { set_intrinsic_width(font().width(dom_node().value())); set_intrinsic_height(font().glyph_height()); } void ButtonBox::paint(PaintContext& context, Painting::PaintPhase phase) { if (!is_visible()) return; LabelableNode::paint(context, phase); if (phase == Painting::PaintPhase::Foreground) { auto text_rect = enclosing_int_rect(m_paint_box->absolute_rect()); if (m_being_pressed) text_rect.translate_by(1, 1); context.painter().draw_text(text_rect, dom_node().value(), font(), Gfx::TextAlignment::Center, computed_values().color()); } } void ButtonBox::handle_mousedown(Badge, const Gfx::IntPoint&, unsigned button, unsigned) { if (button != GUI::MouseButton::Primary || !dom_node().enabled()) return; m_being_pressed = true; set_needs_display(); m_tracking_mouse = true; browsing_context().event_handler().set_mouse_event_tracking_layout_node(this); } void ButtonBox::handle_mouseup(Badge, const Gfx::IntPoint& position, unsigned button, unsigned) { if (!m_tracking_mouse || button != GUI::MouseButton::Primary || !dom_node().enabled()) return; // NOTE: Handling the click may run arbitrary JS, which could disappear this node. NonnullRefPtr protected_this = *this; NonnullRefPtr protected_browsing_context = browsing_context(); bool is_inside_node_or_label = enclosing_int_rect(m_paint_box->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) dom_node().did_click_button({}); m_being_pressed = false; m_tracking_mouse = false; protected_browsing_context->event_handler().set_mouse_event_tracking_layout_node(nullptr); } void ButtonBox::handle_mousemove(Badge, const Gfx::IntPoint& position, unsigned, unsigned) { if (!m_tracking_mouse || !dom_node().enabled()) return; bool is_inside_node_or_label = enclosing_int_rect(m_paint_box->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