summaryrefslogtreecommitdiff
path: root/Libraries/LibWeb/Layout
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2020-09-11 18:17:39 +0200
committerAndreas Kling <kling@serenityos.org>2020-09-11 18:42:43 +0200
commitf2431adf471af027b04b635f6991b708bf0627c1 (patch)
treec35e167cf89ce9aa28df612f1678deba122090f5 /Libraries/LibWeb/Layout
parentd6889ecf35949ef1c5f9b9a794db2f33c9106a45 (diff)
downloadserenity-f2431adf471af027b04b635f6991b708bf0627c1.zip
LibWeb: Add basic support for <input type=checkbox>
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.
Diffstat (limited to 'Libraries/LibWeb/Layout')
-rw-r--r--Libraries/LibWeb/Layout/LayoutCheckBox.cpp105
-rw-r--r--Libraries/LibWeb/Layout/LayoutCheckBox.h61
-rw-r--r--Libraries/LibWeb/Layout/LayoutNode.h1
3 files changed, 167 insertions, 0 deletions
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; }