From f2431adf471af027b04b635f6991b708bf0627c1 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 11 Sep 2020 18:17:39 +0200 Subject: LibWeb: Add basic support for This is implemented entirely inside LibWeb, there is no GUI::CheckBox widget instantiated, unlike other input types. All input types should be moved to this new style of implementation. --- Libraries/LibWeb/Layout/LayoutCheckBox.cpp | 105 +++++++++++++++++++++++++++++ Libraries/LibWeb/Layout/LayoutCheckBox.h | 61 +++++++++++++++++ Libraries/LibWeb/Layout/LayoutNode.h | 1 + 3 files changed, 167 insertions(+) create mode 100644 Libraries/LibWeb/Layout/LayoutCheckBox.cpp create mode 100644 Libraries/LibWeb/Layout/LayoutCheckBox.h (limited to 'Libraries/LibWeb/Layout') diff --git a/Libraries/LibWeb/Layout/LayoutCheckBox.cpp b/Libraries/LibWeb/Layout/LayoutCheckBox.cpp new file mode 100644 index 0000000000..febab14800 --- /dev/null +++ b/Libraries/LibWeb/Layout/LayoutCheckBox.cpp @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2020, Andreas Kling + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include + +namespace Web { + +LayoutCheckBox::LayoutCheckBox(DOM::Document& document, HTML::HTMLInputElement& element, NonnullRefPtr style) + : LayoutReplaced(document, element, move(style)) +{ +} + +LayoutCheckBox::~LayoutCheckBox() +{ +} + +void LayoutCheckBox::layout(LayoutMode layout_mode) +{ + set_has_intrinsic_width(true); + set_has_intrinsic_height(true); + set_intrinsic_width(13); + set_intrinsic_height(13); + LayoutReplaced::layout(layout_mode); +} + +void LayoutCheckBox::paint(PaintContext& context, PaintPhase phase) +{ + if (!is_visible()) + return; + + LayoutReplaced::paint(context, phase); + + if (phase == PaintPhase::Foreground) { + Gfx::StylePainter::paint_check_box(context.painter(), enclosing_int_rect(absolute_rect()), context.palette(), node().enabled(), node().checked(), m_being_pressed); + } +} + +void LayoutCheckBox::handle_mousedown(Badge, const Gfx::IntPoint&, unsigned button, unsigned) +{ + if (button != GUI::MouseButton::Left) + return; + + m_being_pressed = true; + set_needs_display(); + + m_tracking_mouse = true; + frame().event_handler().set_mouse_event_tracking_layout_node(this); +} + +void LayoutCheckBox::handle_mouseup(Badge, const Gfx::IntPoint& position, unsigned button, unsigned) +{ + if (!m_tracking_mouse || button != GUI::MouseButton::Left) + return; + + bool is_inside = enclosing_int_rect(absolute_rect()).contains(position); + if (is_inside) + node().set_checked(!node().checked()); + + m_being_pressed = false; + m_tracking_mouse = false; + frame().event_handler().set_mouse_event_tracking_layout_node(nullptr); +} + +void LayoutCheckBox::handle_mousemove(Badge, const Gfx::IntPoint& position, unsigned, unsigned) +{ + if (!m_tracking_mouse) + return; + + bool is_inside = enclosing_int_rect(absolute_rect()).contains(position); + if (m_being_pressed == is_inside) + return; + + m_being_pressed = is_inside; + set_needs_display(); +} + +} diff --git a/Libraries/LibWeb/Layout/LayoutCheckBox.h b/Libraries/LibWeb/Layout/LayoutCheckBox.h new file mode 100644 index 0000000000..8b9644a0d6 --- /dev/null +++ b/Libraries/LibWeb/Layout/LayoutCheckBox.h @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2020, Andreas Kling + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include +#include + +namespace Web { + +class LayoutCheckBox : public LayoutReplaced { +public: + LayoutCheckBox(DOM::Document&, HTML::HTMLInputElement&, NonnullRefPtr); + virtual ~LayoutCheckBox() override; + + virtual void layout(LayoutMode = LayoutMode::Default) override; + virtual void paint(PaintContext&, PaintPhase) override; + + const HTML::HTMLInputElement& node() const { return static_cast(LayoutReplaced::node()); } + HTML::HTMLInputElement& node() { return static_cast(LayoutReplaced::node()); } + +private: + virtual const char* class_name() const override { return "LayoutCheckBox"; } + virtual bool is_check_box() const override { return true; } + virtual bool wants_mouse_events() const override { return true; } + virtual void handle_mousedown(Badge, const Gfx::IntPoint&, unsigned button, unsigned modifiers) override; + virtual void handle_mouseup(Badge, const Gfx::IntPoint&, unsigned button, unsigned modifiers) override; + virtual void handle_mousemove(Badge, const Gfx::IntPoint&, unsigned buttons, unsigned modifiers) override; + + bool m_being_pressed { false }; + bool m_tracking_mouse { false }; +}; + +} + +AK_BEGIN_TYPE_TRAITS(Web::LayoutCheckBox) +static bool is_type(const Web::LayoutNode& layout_node) { return layout_node.is_check_box(); } +AK_END_TYPE_TRAITS() diff --git a/Libraries/LibWeb/Layout/LayoutNode.h b/Libraries/LibWeb/Layout/LayoutNode.h index b9cc0136ad..42b2b094e8 100644 --- a/Libraries/LibWeb/Layout/LayoutNode.h +++ b/Libraries/LibWeb/Layout/LayoutNode.h @@ -92,6 +92,7 @@ public: virtual bool is_table_cell() const { return false; } virtual bool is_table_row_group() const { return false; } virtual bool is_break() const { return false; } + virtual bool is_check_box() const { return false; } bool has_style() const { return m_has_style; } bool is_inline() const { return m_inline; } -- cgit v1.2.3