diff options
-rw-r--r-- | Libraries/LibWeb/CMakeLists.txt | 1 | ||||
-rw-r--r-- | Libraries/LibWeb/HTML/HTMLInputElement.cpp | 19 | ||||
-rw-r--r-- | Libraries/LibWeb/HTML/HTMLInputElement.h | 8 | ||||
-rw-r--r-- | Libraries/LibWeb/HTML/HTMLInputElement.idl | 1 | ||||
-rw-r--r-- | Libraries/LibWeb/Layout/LayoutCheckBox.cpp | 105 | ||||
-rw-r--r-- | Libraries/LibWeb/Layout/LayoutCheckBox.h | 61 | ||||
-rw-r--r-- | Libraries/LibWeb/Layout/LayoutNode.h | 1 |
7 files changed, 195 insertions, 1 deletions
diff --git a/Libraries/LibWeb/CMakeLists.txt b/Libraries/LibWeb/CMakeLists.txt index e2ab325e76..c3ad56f489 100644 --- a/Libraries/LibWeb/CMakeLists.txt +++ b/Libraries/LibWeb/CMakeLists.txt @@ -133,6 +133,7 @@ set(SOURCES Layout/LayoutBox.cpp Layout/LayoutBreak.cpp Layout/LayoutCanvas.cpp + Layout/LayoutCheckBox.cpp Layout/LayoutDocument.cpp Layout/LayoutFrame.cpp Layout/LayoutImage.cpp diff --git a/Libraries/LibWeb/HTML/HTMLInputElement.cpp b/Libraries/LibWeb/HTML/HTMLInputElement.cpp index 8af5f42caa..170a10cc40 100644 --- a/Libraries/LibWeb/HTML/HTMLInputElement.cpp +++ b/Libraries/LibWeb/HTML/HTMLInputElement.cpp @@ -30,9 +30,10 @@ #include <LibWeb/DOM/Event.h> #include <LibWeb/HTML/HTMLFormElement.h> #include <LibWeb/HTML/HTMLInputElement.h> +#include <LibWeb/InProcessWebView.h> +#include <LibWeb/Layout/LayoutCheckBox.h> #include <LibWeb/Layout/LayoutWidget.h> #include <LibWeb/Page/Frame.h> -#include <LibWeb/InProcessWebView.h> namespace Web::HTML { @@ -78,6 +79,8 @@ RefPtr<LayoutNode> HTMLInputElement::create_layout_node(const CSS::StyleProperti const_cast<HTMLInputElement*>(this)->dispatch_event(DOM::Event::create("click")); }; widget = button; + } else if (type() == "checkbox") { + return adopt(*new LayoutCheckBox(document(), *this, move(style))); } else { auto& text_box = page_view.add<GUI::TextBox>(); text_box.set_text(value()); @@ -99,4 +102,18 @@ RefPtr<LayoutNode> HTMLInputElement::create_layout_node(const CSS::StyleProperti return adopt(*new LayoutWidget(document(), *this, *widget)); } +void HTMLInputElement::set_checked(bool checked) +{ + if (m_checked == checked) + return; + m_checked = checked; + if (layout_node()) + layout_node()->set_needs_display(); +} + +bool HTMLInputElement::enabled() const +{ + return !has_attribute(HTML::AttributeNames::disabled); +} + } diff --git a/Libraries/LibWeb/HTML/HTMLInputElement.h b/Libraries/LibWeb/HTML/HTMLInputElement.h index 0bec44e8de..a17417fc07 100644 --- a/Libraries/LibWeb/HTML/HTMLInputElement.h +++ b/Libraries/LibWeb/HTML/HTMLInputElement.h @@ -42,6 +42,14 @@ public: String type() const { return attribute(HTML::AttributeNames::type); } String value() const { return attribute(HTML::AttributeNames::value); } String name() const { return attribute(HTML::AttributeNames::name); } + + bool checked() const { return m_checked; } + void set_checked(bool); + + bool enabled() const; + +private: + bool m_checked { false }; }; } diff --git a/Libraries/LibWeb/HTML/HTMLInputElement.idl b/Libraries/LibWeb/HTML/HTMLInputElement.idl index 84004409e8..fa77917cc7 100644 --- a/Libraries/LibWeb/HTML/HTMLInputElement.idl +++ b/Libraries/LibWeb/HTML/HTMLInputElement.idl @@ -11,4 +11,5 @@ interface HTMLInputElement : HTMLElement { [Reflect=dirname] attribute DOMString dirName; [Reflect=value] attribute DOMString defaultValue; + attribute boolean checked; } 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 <kling@serenityos.org> + * 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 <LibGUI/Event.h> +#include <LibGUI/Painter.h> +#include <LibGfx/Font.h> +#include <LibGfx/StylePainter.h> +#include <LibWeb/Layout/LayoutCheckBox.h> +#include <LibWeb/Page/Frame.h> + +namespace Web { + +LayoutCheckBox::LayoutCheckBox(DOM::Document& document, HTML::HTMLInputElement& element, NonnullRefPtr<CSS::StyleProperties> 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<EventHandler>, 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<EventHandler>, 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<EventHandler>, 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 <kling@serenityos.org> + * 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 <LibWeb/HTML/HTMLInputElement.h> +#include <LibWeb/Layout/LayoutReplaced.h> + +namespace Web { + +class LayoutCheckBox : public LayoutReplaced { +public: + LayoutCheckBox(DOM::Document&, HTML::HTMLInputElement&, NonnullRefPtr<CSS::StyleProperties>); + virtual ~LayoutCheckBox() override; + + virtual void layout(LayoutMode = LayoutMode::Default) override; + virtual void paint(PaintContext&, PaintPhase) override; + + const HTML::HTMLInputElement& node() const { return static_cast<const HTML::HTMLInputElement&>(LayoutReplaced::node()); } + HTML::HTMLInputElement& node() { return static_cast<HTML::HTMLInputElement&>(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<EventHandler>, const Gfx::IntPoint&, unsigned button, unsigned modifiers) override; + virtual void handle_mouseup(Badge<EventHandler>, const Gfx::IntPoint&, unsigned button, unsigned modifiers) override; + virtual void handle_mousemove(Badge<EventHandler>, 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; } |